NewsNewsFeaturesDownloadsDevelopmentSupportForumDocumentsAbout Us

adminaddtemplateaction.class.php

查看本檔案說明文件.
00001 <?php
00002 
00003     include_once( PLOG_CLASS_PATH.'class/action/admin/siteadminaction.class.php' );
00004     include_once( PLOG_CLASS_PATH.'class/view/admin/adminsitetemplateslistview.class.php' );
00005     include_once( PLOG_CLASS_PATH.'class/view/admin/admintemplatedview.class.php' );
00006     include_once( PLOG_CLASS_PATH.'class/file/unpacker/unpacker.class.php' );
00007     include_once( PLOG_CLASS_PATH.'class/data/validator/templatevalidator.class.php' );
00008     include_once( PLOG_CLASS_PATH.'class/data/validator/stringvalidator.class.php' );
00009     include_once( PLOG_CLASS_PATH.'class/template/templatesandbox.class.php' );
00010     include_once( PLOG_CLASS_PATH.'class/file/fileuploads.class.php' );
00011     include_once( PLOG_CLASS_PATH.'class/template/templatesets/templatesetstorage.class.php' );
00012     include_once( PLOG_CLASS_PATH.'class/template/templatesets/templatefinder.class.php' ); 
00013     include_once( PLOG_CLASS_PATH.'class/data/validator/uploadvalidator.class.php' );
00014 
00021     class AdminAddTemplateAction extends SiteAdminAction
00022     {
00023 
00024         function AdminAddTemplateAction( $actionInfo, $request )
00025         {
00026             $this->SiteAdminAction( $actionInfo, $request );
00027 
00028             // decide what to do based on which submit button was pressed
00029             if( $this->_request->getValue( "addTemplateUpload" ) != "" )
00030                 $this->_op = "addTemplateUpload";
00031             else
00032                 $this->_op = "scanTemplates";
00033         }
00034 
00038         function _checkTemplateSandboxResult( $result )
00039         {
00040             switch( $result ) {
00041                 case ERROR_TEMPLATE_NOT_INSIDE_FOLDER:
00042                     $errorMessage = $this->_locale->tr('error_template_not_inside_folder');
00043                     break;
00044                 case ERROR_MISSING_BASE_FILES:
00045                     $errorMessage =  $this->_locale->tr('error_missing_base_files');
00046                     break;
00047                 case TEMPLATE_SANDBOX_ERROR_UNPACKING:
00048                     $errorMessage =  $this->_locale->tr('error_unpacking');
00049                     break;
00050                 case TEMPLATE_SANDBOX_ERROR_FORBIDDEN_EXTENSIONS:
00051                     $errorMessage =  $this->_locale->tr('error_forbidden_extensions');
00052                     break;
00053                 case TEMPLATE_SANDBOX_ERROR_CREATING_WORKING_FOLDER:
00054                     $errorMessage = $this->_locale->tr('error_creating_working_folder');
00055                     break;
00056                 default:
00057                     $errorMessage = $this->_locale->pr('error_checking_template', $result);
00058                     break;
00059             }
00060 
00061             return $errorMessage;
00062         }
00063 
00067         function _performUploadTemplate()
00068         {
00069             // handle the uploaded file
00070             $files    = HttpVars::getFiles();
00071             $uploads  = new FileUploads( $files );
00072             
00073             if( count($files) == 0 || $files["templateFile"]["name"] == "") {
00074                 $this->_view = new AdminTemplatedView( $this->_blogInfo, "newglobaltemplate" );
00075                 $this->_view->setValue( "templateFolder", TemplateSetStorage::getBaseTemplateFolder());
00076                 $this->_view->setErrorMessage( $this->_locale->tr("error_must_upload_file"));
00077                 $this->setCommonData();
00078                 return false;
00079             }            
00080 
00081             $config =& Config::getConfig();
00082 
00083             $tmpFolder = $config->getValue( 'temp_folder' );
00084 
00085             // move it to the temporary folder
00086             $result = $uploads->process( $tmpFolder );
00087 
00088             // and from there, unpack it
00089             $upload   = new FileUpload( $files['templateFile'] );
00090 
00091             $templateSandbox = new TemplateSandbox();
00092             $valid = $templateSandbox->checkTemplateSet( $upload->getFileName(), $tmpFolder.'/');
00093 
00094             if( $valid < 0 ) {
00095                 $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
00096                 $this->_view->setErrorMessage( $this->_checkTemplateSandboxResult( $valid ));
00097                 $this->setCommonData();
00098                 return false;
00099             }
00100 
00101             // the template was ok, so then we can proceed and move it to the main
00102             // template folder, add it to our array of templates
00103 
00104             //
00105             // :KLUDGE:
00106             //
00107             // maybe we should simply move the files rather than unpacking the whole
00108             // thing again, but this indeed makes things easier! ;)
00109             $unpacker = new Unpacker();
00110             $templateFolder = $config->getValue( 'template_folder' );
00111             $fileToUnpack = $tmpFolder.'/'.$upload->getFileName();
00112             if( !$unpacker->unpack( $fileToUnpack, $templateFolder )) {
00113                 $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
00114                 $tf = new Textfilter();
00115                 $this->_view->setErrorMessage( $this->_locale->pr('error_installing_template', $tf->filterAllHtml($upload->getFileName())));
00116                 $this->setCommonData();
00117                 return false;
00118             }
00119 
00120             // if the template set was installed ok in the template folder, we can record
00121             // it as a valid set
00122             $ts = new TemplateSetStorage();
00123             $fileParts = explode( ".", $upload->getFileName());
00124             $templateName = $fileParts[0];
00125             $ts->addTemplate( $templateName );
00126 
00127             $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
00128             $this->_view->setSuccessMessage( $this->_locale->pr('template_installed_ok', $templateName));
00129             $this->setCommonData();
00130 
00131             return true;
00132         }
00133 
00134 
00135         //
00136         // adds a template manually
00137         //
00138         function _addTemplateCode( $templateName )
00139         {
00140 
00141             $ts = new TemplateSetStorage();
00142 
00143             // make sure that the template is valid
00144             $templateSandbox = new TemplateSandbox();
00145             $valid = $templateSandbox->checkTemplateFolder( $templateName, $ts->getBaseTemplateFolder());
00146             if( $valid < 0 ) {
00147                 $this->_errorMessage = $this->_locale->pr( 'error_installing_template', $templateName ).': '.$this->_checkTemplateSandboxResult( $valid ).'<br/>';              
00148                 $result = false;
00149             }
00150             else {
00151                 // otherwise, we can add it without problems
00152                 $ts->addTemplate( $templateName );
00153                 $this->_successMessage = $this->_locale->pr( 'template_installed_ok', $templateName).'<br/>';
00154                 $result = true;
00155             }
00156 
00157             $this->setCommonData();
00158             return $result;
00159         }
00160         
00166         function _performScanTemplateFolder()
00167         {
00168             // set up the view
00169             $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );      
00170         
00171             // and tell the template finder to find any new template file...
00172             $tf = new TemplateFinder();
00173             $newTemplates = $tf->find();
00174             
00175             $this->_errorMessage = "";
00176             $this->_successMessage = "";
00177             
00178             if( count($newTemplates) == 0 ) {
00179                 // no new templates found
00180                 $this->_errorMessage = $this->_locale->tr( 'error_no_new_templates_found' );
00181             }
00182             else {
00183                 // now add each one of the new ones
00184                 foreach( $newTemplates as $newTemplate ) {
00185                     $this->_addTemplateCode( $newTemplate );
00186                 }
00187             }
00188             
00189             // set the success and error messages, if any
00190             if( $this->_errorMessage != '' ) $this->_view->setErrorMessage( $this->_errorMessage );
00191             if( $this->_successMessage != '' ) $this->_view->setSuccessMessage( $this->_successMessage );
00192             
00193             return true;
00194         }
00195 
00199         function perform()
00200         {
00201             if( $this->_op == 'addTemplateUpload' )
00202                 $result = $this->_performUploadTemplate();
00203             elseif( $this->_op == 'scanTemplates' )
00204                 $result = $this->_performScanTemplateFolder();
00205             else {
00206                 throw( new Exception( 'You shouldn\'t be seeing this!!! :)' ));
00207                 die();
00208             }
00209 
00210             $this->setCommonData();
00211 
00212             return $result;
00213         }
00214     }
00215 ?>