Bläddra i källkod

#53 - cron pruning refactoring, comments text saving,other small fixes

Alex Trofimov 12 år sedan
förälder
incheckning
181ae1e2aa

+ 0 - 16
404.html

@@ -1,16 +0,0 @@
-<!-- TODO: remove this file -->
-
-<html>
-
-    <body>
-
-        <div style="padding:20px; margin:100px; border:1px solid red; text-align:center; font-size:20px; font-family:Arial;">
-
-            404. Page Not Found
-
-        </div>
-
-    </body>
-
-</html>
-

+ 13 - 8
inc/classes/BxDolAcl.php

@@ -194,8 +194,6 @@ class BxDolAcl extends BxDol implements iBxDolSingleton {
     	$aResult = array();
     	$aLangFileParams = array();
     
-    	$dateFormat = "F j, Y, g:i a";	// used when displaying error messages // TODO: use standard format
-    
     	$iProfileId = (int)$iProfileId;
     	$iActionId = (int)$iActionId;
     	$bPerformAction = $bPerformAction ? true : false;
@@ -230,7 +228,7 @@ class BxDolAcl extends BxDol implements iBxDolSingleton {
     	 * Check fixed period limitations if present (also for non-members)
     	 */
     	if($aAction['allowed_period_start'] && time() < $aAction['allowed_period_start']) {
-    		$aLangFileParams[CHECK_ACTION_LANG_FILE_BEFORE] = date($dateFormat, $aAction['allowed_period_start']);
+    		$aLangFileParams[CHECK_ACTION_LANG_FILE_BEFORE] = bx_time_js($aAction['allowed_period_start'], BX_FORMAT_DATE_TIME);
 
     		$aResult[CHECK_ACTION_RESULT] = CHECK_ACTION_RESULT_NOT_ALLOWED_BEFORE;
     		$aResult[CHECK_ACTION_MESSAGE] = _t_ext(CHECK_ACTION_MESSAGE_NOT_ALLOWED_BEFORE, $aLangFileParams);
@@ -239,7 +237,7 @@ class BxDolAcl extends BxDol implements iBxDolSingleton {
     	}
 
     	if($aAction['allowed_period_end'] && time() > $aAction['allowed_period_end']) {
-    		$aLangFileParams[CHECK_ACTION_LANG_FILE_AFTER] = date($dateFormat, $aAction['allowed_period_end']);
+    		$aLangFileParams[CHECK_ACTION_LANG_FILE_AFTER] = bx_time_js($aAction['allowed_period_end'], BX_FORMAT_DATE_TIME);
 
     		$aResult[CHECK_ACTION_RESULT] = CHECK_ACTION_RESULT_NOT_ALLOWED_AFTER;
     		$aResult[CHECK_ACTION_MESSAGE] = _t_ext(CHECK_ACTION_MESSAGE_NOT_ALLOWED_AFTER, $aLangFileParams);
@@ -534,11 +532,10 @@ class BxDolAcl extends BxDol implements iBxDolSingleton {
     	
     	// Send notification
     	bx_import('BxDolEmailTemplates');
-        $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_MemChanged', array('membership_level' => $aLevel['name']), 0, $iProfileId);
-        if (!$aTemplate)
-            trigger_error('Email template or translation missing: t_MemChanged', E_USER_ERROR);
+        $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_MemChanged', array('membership_level' => $aLevel['name']), 0, $iProfileId);            
+        if ($aTemplate)
+            sendMail($sProfileEmail, $aTemplate['Subject'], $aTemplate['Body']);
 
-        sendMail($sProfileEmail, $aTemplate['Subject'], $aTemplate['Body']);
     	return true;
     }
 
@@ -606,6 +603,14 @@ class BxDolAcl extends BxDol implements iBxDolSingleton {
         return !empty($iResult);
     }
 
+    /**
+     * clear expired membership levels
+     */
+    public function maintenance () {
+        $iDaysToCleanMemLevels = (int) getParam("db_clean_mem_levels");
+        return $this->oDb->maintenance ($iDaysToCleanMemLevels);
+    }
+
     protected function getMemberMembershipInfoCurrent($iProfileId, $iTime = 0) {
 
         $aMemLevel = false;

+ 7 - 0
inc/classes/BxDolAclQuery.php

@@ -254,6 +254,13 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton {
         $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `IDMember`=?" . ($bAll ? " AND (`DateExpires` IS NULL OR `DateExpires`>NOW())" : ""), $iProfileId);
         return (int)$this->query($sQuery) > 0;
     }
+
+    function maintenance($iDaysToCleanMemLevels) {
+        $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL ? DAY", $iDaysToCleanMemLevels);
+        if ($iDeleteMemLevels = $this->query($sQuery))
+            $this->query("OPTIMIZE TABLE `sys_acl_levels_members`");
+        return $iDeleteMemLevels;
+    }
 }
 
 /** @} */

+ 3 - 5
inc/classes/BxDolCmts.php

@@ -834,28 +834,26 @@ class BxDolCmts extends BxDol implements iBxDolReplaceable
     protected function _prepareTextForEdit ($s)
     {
         if ($this->isNl2br())
-            return str_replace('<br />', "", $s);
+            return htmlspecialchars_decode(str_replace('<br />', "", $s));
 
         return $s;
     }
 
     protected function _prepareTextForSave ($s) 
     {
-    	$iDataAction = $this->isNl2br() ? BX_DATA_TEXT_MULTILINE : BX_DATA_TEXT; // TODO: make sure that it is processed before output !
+    	$iDataAction = $this->isNl2br() ? BX_DATA_TEXT_MULTILINE : BX_DATA_HTML;
 		return bx_process_input($s, $iDataAction);
     }
 
     protected function _prepareTextForOutput ($s)
     {
     	$sHttp = '';
-    	$iDataAction = $this->isNl2br() ? BX_DATA_TEXT_MULTILINE : BX_DATA_TEXT;
 		$sPattern = "/((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*\.)+(aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|[a-z]{2})|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?/";
-
     	$aMatches = array();
     	if(preg_match($sPattern, $s, $aMatches) && empty($aMatches[1]))
     		$sHttp = 'http://';
 
-		$s = bx_process_output($s, $iDataAction);
+		$s = bx_process_output($s, BX_DATA_HTML);
 		$s = preg_replace($sPattern, '<a href="' . $sHttp . '$0" target="_blank">$0</a>', $s);
 
 		return $s; 

+ 62 - 74
inc/classes/BxDolCronCmd.php

@@ -7,47 +7,49 @@
  * @{
  */
 
-// TODO: make alert for database pruning
-// TODO: db funtions 2 class function + prepare statements
-// TODO: other refactoring
-
 bx_import('BxDolCron');
 
 class BxDolCronCmd extends BxDolCron 
 {
-    protected function finish() 
+    protected function start() 
     {
-        if ($output = ob_get_clean())
-            sendMail(getParam('site_email'), getParam('site_title') . ": Periodic Report", $output, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template
+        set_time_limit(0);
+        ignore_user_abort();
+        ob_start();
     }
 
-    protected function clean_database()
+    protected function finish() 
     {
-        $oDb = BxDolDb::getInstance();
+        bx_alert('system', 'pruning', 0);
+
+        if (!($sOutput = ob_get_clean()))
+            return;
 
-        $db_clean_mem_levels = (int) getParam("db_clean_mem_levels");
+    	bx_import('BxDolEmailTemplates');
+        $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Pruning', array('pruning_output' => $sOutput, 'site_title' => getParam('site_title')), 0, 0);
+        if ($aTemplate)
+            sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY);
+    }
 
-        //clear from `sys_acl_levels_members`
-        if ($oDb->query("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL $db_clean_mem_levels DAY"))
-            $oDb->query("OPTIMIZE TABLE `sys_acl_levels_members`");
+    /**
+     * Clean database by deleting some expired data
+     */
+    protected function cleanDatabase()
+    {
+        // clean expired membership levels
+        bx_import('BxDolAcl');
+        $oAcl = BxDolAcl::getInstance();
+        $iDeleteMemLevels = $oAcl ? $oAcl->maintenance() : 0;
 
         //--- Clean sessions ---//
         bx_import('BxDolSession');
         $oSession = BxDolSession::getInstance();
-        $iSessions = $oSession->oDb->deleteExpired();
+        $iSessions = $oSession ? $oSession->maintenance() : 0;
 
         // clean old views
         bx_import('BxDolView');
         $iDeletedViews = BxDolView::maintenance ();
 
-        // clean old votes
-        bx_import('BxDolVote');
-        BxDolVote::maintenance();
-
-        // clean comments ratings
-        //bx_import('BxDolCmts');
-        //BxDolCmts::maintenance(); // TODO: fix it
-
         // clean storage engine expired private file tokens
         bx_import('BxDolStorage');
         $iDeletedExpiredTokens = BxDolStorage::pruning();
@@ -61,72 +63,58 @@ class BxDolCronCmd extends BxDolCron
         $oKey = BxDolKey::getInstance();
         $iDeletedKeys = $oKey ? $oKey->prune() : 0;
 
-        echo "\n- Database cleaning -\n";
-        echo "Deleted sessions: $iSessions\n";
-        echo "Deleted views: $iDeletedViews\n";
-        echo "Storage expired tokens: $iDeletedExpiredTokens\n";
-        echo "Deleted outdated transcoded images: $iDeletedTranscodedImages\n";
-        echo "Deleted expired keys: $iDeletedKeys\n";
+        // clean old votes
+        bx_import('BxDolVote');
+        $iDeletedVotes = BxDolVote::maintenance();
+
+        // clean comments ratings
+        //bx_import('BxDolCmts');
+        //BxDolCmts::maintenance(); // TODO: fix it
+
+        echo _t('_sys_pruning_db', $iDeleteMemLevels, $iSessions, $iDeletedViews, $iDeletedVotes, $iDeletedKeys, $iDeletedExpiredTokens, $iDeletedTranscodedImages);
     }
 
-    protected function del_old_all_files() 
+    /**
+     * Clean tmp folders (tmp, cache) by deleting old files (by default older than 1 month)
+     */
+    protected function cleanTmpFolders() 
     {
-        $num_tmp = 0;
-        $num_del = 0;
-
-        $file_life = 86400;  // one day
-        $dirToClean = array();
-        $dirToClean[] = BxDolConfig::getInstance()->get('path_dynamic', 'tmp');
-        $dirToClean[] = BX_DIRECTORY_PATH_CACHE;
-
-        foreach( $dirToClean as $value )
-        {
-            if ( !( $lang_dir = opendir( $value ) ) )
-            {
+        $iTmpFileLife = 2592000;  // one month
+        $aDirsToClean = array(
+            BX_DIRECTORY_PATH_TMP,
+            BX_DIRECTORY_PATH_CACHE,
+            BX_DIRECTORY_PATH_CACHE_PUBLIC,
+        );
+
+        $iNumTmp = 0;
+        $iNumDel = 0;
+
+        foreach ($aDirsToClean as $sDir) {
+            if (!($h = opendir($sDir)))
                 continue;
-            }
-            else
-            {
-                while ($lang_file = readdir( $lang_dir ))
-                {
-                    $diff = time() - filectime( $value . $lang_file);
-                    if ( $diff > $file_life && '.' != $lang_file && '..' != $lang_file && '.htaccess' !== $lang_file )
-                    {
-                        @unlink ($value . $lang_file);
-                        ++$num_del;
-                    }
-                    ++$num_tmp;
+
+            while ($sFile = readdir($h)) {
+                $diff = time() - filectime($sDir . $sFile);
+                if ($diff > $iTmpFileLife && '.' != $sFile && '..' != $sFile && '.htaccess' !== $sFile) {
+                    @unlink ($sDir . $sFile);
+                    ++$iNumDel;
                 }
-                closedir( $lang_dir );
+                ++$iNumTmp;
             }
-        }
+            closedir($h);
 
-        echo "\n- Temporary files check -\n";
+        }
 
-        echo "Total temp files: $num_tmp\n";
-        echo "Deleted temp files: $num_del\n";
+        echo _t('_sys_pruning_files', $iNumTmp, $iNumDel);
     }
 
     public function processing()    
     {
-        set_time_limit(0);
-        ignore_user_abort();
-
-        ob_start();
-
-        $iDay = date( "d" );
-        if (getParam( "cmdDay" ) == $iDay) {
-            echo "Already done today, bailing out\n";
-            $this->finish();
-            return;
-        }
-
-        setParam("cmdDay", $iDay);
+        $this->start();
 
-        // clear tmp folder 
-        $this->del_old_all_files();
+        $this->cleanTmpFolders();
 
-        $this->clean_database();
+        $this->cleanDatabase();
 
         $this->finish();
     }

+ 5 - 1
inc/classes/BxDolSession.php

@@ -141,7 +141,11 @@ class BxDolSession extends BxDol implements iBxDolSingleton {
         return $mixedValue;
     }
 
-    private function save() {
+    public function maintenance() {
+        return $this->oDb->deleteExpired();
+    }
+
+    protected function save() {
         if($this->iUserId == 0)
             $this->iUserId = getLoggedId();
 

+ 1 - 1
inc/classes/BxDolTemplate.php

@@ -903,7 +903,7 @@ class BxDolTemplate extends BxDol implements iBxDolSingleton {
                 break;
             case 'page_header':
                 if(isset($this->aPage['header']))
-                    $sRet = bx_process_output($this->aPage['header']);
+                    $sRet = bx_process_output(strip_tags($this->aPage['header']));
                 break;
             case 'page_header_text':
                 if(isset($this->aPage['header_text']))

+ 1 - 1
inc/utils.inc.php

@@ -156,7 +156,7 @@ function bx_process_input ($mixedData, $iDataType = BX_DATA_TEXT, $mixedParams =
     case BX_DATA_HTML:
         return clear_xss($mixedData);
     case BX_DATA_TEXT_MULTILINE:
-        return nl2br(strip_tags($mixedData));
+        return nl2br(htmlspecialchars_adv($mixedData));
     case BX_DATA_TEXT:
     default:
         return $mixedData;

+ 2 - 2
install/sql/system.sql

@@ -151,7 +151,8 @@ INSERT INTO `sys_email_templates` (`Module`, `NameSystem`, `Name`, `Subject`, `B
 ('system', '_sys_et_txt_name_system_mem_expiration', 't_MemExpiration', '_sys_et_txt_subject_mem_expiration', '_sys_et_txt_body_mem_expiration'),
 ('system', '_sys_et_txt_name_system_mem_changed', 't_MemChanged', '_sys_et_txt_subject_mem_changed', '_sys_et_txt_body_mem_changed'),
 ('system', '_sys_et_txt_name_system_comment_replied', 't_CommentReplied', '_sys_et_txt_subject_comment_replied', '_sys_et_txt_body_comment_replied'),
-('system', '_sys_et_txt_name_system_delayed_module_uninstall', 't_DelayedModuleUninstall', '_sys_et_txt_subject_delayed_module_uninstall', '_sys_et_txt_body_delayed_module_uninstall');
+('system', '_sys_et_txt_name_system_delayed_module_uninstall', 't_DelayedModuleUninstall', '_sys_et_txt_subject_delayed_module_uninstall', '_sys_et_txt_body_delayed_module_uninstall'),
+('system', '_sys_et_txt_name_system_pruning', 't_Pruning', '_sys_et_txt_subject_pruning', '_sys_et_txt_body_pruning');
 
 -- --------------------------------------------------------
 
@@ -216,7 +217,6 @@ INSERT INTO `sys_options_categories`(`type_id`, `name`, `caption`, `hidden`, `or
 SET @iCategoryId = LAST_INSERT_ID();
 
 INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
-(@iCategoryId, 'cmdDay', '', '10', 'digit', '', '', '', 1),
 (@iCategoryId, 'sys_version', '', @sSysVersion, 'digit', '', '', '', 2),
 (@iCategoryId, 'sys_install_time', '', '0', 'digit', '', '', '', 3),
 

+ 22 - 2
modules/boonex/english/data/langs/system.xml

@@ -1322,8 +1322,8 @@
 	<string name="_sys_acl_action_every_period"><![CDATA[ every {4} hours]]></string>
 	<string name="_sys_acl_action_limit_reached"><![CDATA[You have reached your limit for now. Your current membership (<b>{2}</b>) allows you to {1} no more than {3} times]]></string>
 	<string name="_sys_acl_action_not_allowed"><![CDATA[Your current membership (<b>{2}</b>) doesn't allow you to <b>{1}</b>.]]></string>
-	<string name="_sys_acl_action_not_allowed_after"><![CDATA[Your current membership (<b>{2}</b>) doesn't allow you to <b>{1}</b> since <b>{5}</b>.]]></string>
-	<string name="_sys_acl_action_not_allowed_before"><![CDATA[Your current membership (<b>{2}</b>) doesn't allow you to <b>{1}</b> until <b>{6}</b>.]]></string>
+	<string name="_sys_acl_action_not_allowed_after"><![CDATA[Your current membership (<b>{2}</b>) stopped to allow you to <b>{1}</b> <b>{5}</b>.]]></string>
+	<string name="_sys_acl_action_not_allowed_before"><![CDATA[Your current membership (<b>{2}</b>) will allow you to <b>{1}</b> <b>{6}</b>.]]></string>
 	<string name="_sys_acl_action_pending"><![CDATA[You are currently not an active member. Please, ask the site <a href="mailto:{7}">administrator</a> to make you an active member so you would be able to <b>{1}</b>.]]></string>
 	<string name="_sys_acl_action_suspended"><![CDATA[You can't <b>{1}</b> becuase your profile was suspended, if you don't know why - try to send message to <a href="mailto:{7}">administrator</a>.]]></string>
 	<string name="_sys_acl_action_unauthenticated"><![CDATA[Not logged in users can't <b>{1}</b>.]]></string>
@@ -1460,6 +1460,9 @@ If it is not enabled then please consider implement this optimization, since it
 <p>Hello!</p>
 <p>Your new password is:</p>
 <p><b>{password}</b></p>
+<bx_include_auto:_email_footer.html />]]></string>
+	<string name="_sys_et_txt_body_pruning"><![CDATA[<bx_include_auto:_email_header.html />
+<p>{pruning_output}</p>
 <bx_include_auto:_email_footer.html />]]></string>
 	<string name="_sys_et_txt_name_system_activation"><![CDATA[Profile activation message]]></string>
 	<string name="_sys_et_txt_name_system_admin_email"><![CDATA[Message sending from the Admin Panel]]></string>
@@ -1470,6 +1473,7 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_sys_et_txt_name_system_mem_changed"><![CDATA[The letter is sent to members whose membership level was changed]]></string>
 	<string name="_sys_et_txt_name_system_mem_expiration"><![CDATA[Membership expiration letter sent to members whose membership level expires]]></string>
 	<string name="_sys_et_txt_name_system_password_reset"><![CDATA[New password message]]></string>
+	<string name="_sys_et_txt_name_system_pruning"><![CDATA[Pruning result]]></string>
 	<string name="_sys_et_txt_subject_activation"><![CDATA[Profile status was changed to Active]]></string>
 	<string name="_sys_et_txt_subject_admin_email"><![CDATA[Message from <SiteName> Admin: <MessageSubject>]]></string>
 	<string name="_sys_et_txt_subject_comment_replied"><![CDATA[Your comment was replied]]></string>
@@ -1479,6 +1483,7 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_sys_et_txt_subject_mem_changed"><![CDATA[Your membership level was changed]]></string>
 	<string name="_sys_et_txt_subject_mem_expiration"><![CDATA[Your membership is about to expire]]></string>
 	<string name="_sys_et_txt_subject_password_reset"><![CDATA[New Password]]></string>
+	<string name="_sys_et_txt_subject_pruning"><![CDATA[{site_title} pruning result]]></string>
 	<string name="_sys_et_txt_unsubscribe"><![CDATA[Unsubscribe]]></string>
 	<string name="_sys_form_account"><![CDATA[Account]]></string>
 	<string name="_sys_form_account_input_captcha"><![CDATA[Security Image]]></string>
@@ -1801,6 +1806,21 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_sys_paginate_view_all"><![CDATA[View All]]></string>
 	<string name="_sys_please_select"><![CDATA[Please select...]]></string>
 	<string name="_sys_private"><![CDATA[Private]]></string>
+    <string name="_sys_pruning_db"><![CDATA[<h3>Database pruning:</h3>
+        <ul>
+            <li><b>{0}</b> deleted expired memberhsip levels</li>
+            <li><b>{1}</b> deleted expired sessions</li>
+            <li><b>{2}</b> deleted expired views</li>
+            <li><b>{3}</b> deleted expired votes</li>
+            <li><b>{4}</b> deleted expired keys</li>
+            <li><b>{5}</b> deleted expired storage tokens</li>
+            <li><b>{6}</b> deleted outdated transcoded images</li>
+        </ul>]]></string>
+    <string name="_sys_pruning_files"><![CDATA[<h3>Files pruning:</h3>
+        <ul>
+            <li><b>{0}</b> total</li>
+            <li><b>{1}</b> deleted</li>
+        </ul>]]></string>
 	<string name="_sys_ps_group_title_friends"><![CDATA[Friends]]></string>
 	<string name="_sys_ps_group_title_me_only"><![CDATA[Me only]]></string>
 	<string name="_sys_ps_group_title_members"><![CDATA[Members]]></string>