Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1026028
D292.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jun 13, 6:07 PM
Size
8 KB
Mime Type
text/x-diff
Expires
Sat, Jun 14, 6:07 PM (4 h, 27 m)
Engine
blob
Format
Raw Data
Handle
661675
Attached To
D292: INP-1686 - Don't include timestamp in compiled Admin Skin file
D292.diff
View Options
Index: branches/5.2.x/core/units/helpers/skin_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/skin_helper.php
+++ branches/5.2.x/core/units/helpers/skin_helper.php
@@ -17,87 +17,66 @@
class SkinHelper extends kHelper {
/**
- * Regular expression, used to detect admin skin file among others
- */
- const SKIN_REGEXP = '/admin-(.*)-([\d]+).css/';
-
- /**
* Allows to read some skin fields and build link to admin skin file.
* Will automatically compile skin file, when missing.
*
- * @param Array $params
+ * @param array $params Tag params.
+ *
* @return string
*/
- function AdminSkinTag($params)
+ public function AdminSkinTag(array $params)
{
- if (array_key_exists('type', $params)) {
- // returns given field of skin
- return $this->_getStyleField( $params['type'] );
+ // Returns given field of skin.
+ if ( array_key_exists('type', $params) ) {
+ return $this->_getStyleField($params['type']);
}
- $style_info = $this->_getStyleInfo();
-
- if (file_exists( $this->getSkinPath() )) {
- // returns last compiled skin
+ if ( file_exists($this->getSkinPath()) ) {
$ret = $this->getSkinPath(true);
}
else {
- // search for previously compiled skin
- $last_compiled = $this->_getLastCompiled( mb_strtolower($style_info['Name']) );
- if ($last_compiled) {
- // found
- $ret = $this->getSkinPath(true, $last_compiled);
- }
- else {
- // not found (try to compile on the fly)
- /** @var kDBItem $skin */
- $skin = $this->Application->recallObject('skin.-item', null, Array ('skip_autoload' => true));
-
- $skin->Load(1, 'IsPrimary');
- $last_compiled = $this->compile($skin);
- $ret = $last_compiled ? $this->getSkinPath(true, $last_compiled) : '';
- }
+ /** @var kDBItem $skin */
+ $skin = $this->Application->recallObject('skin.-item', null, array('skip_autoload' => true));
+
+ $skin->Load(1, 'IsPrimary');
+ $ret = $this->compile($skin) ? $this->getSkinPath(true) : '';
}
- if (array_key_exists('file_only', $params) && $params['file_only']) {
+ if ( array_key_exists('file_only', $params) && $params['file_only'] ) {
return $ret;
}
+ $ret .= '?ts=' . adodb_date('Y-m-d_H:i:s', filemtime($this->getSkinPath()));
+
return '<link rel="stylesheet" rev="stylesheet" href="' . $ret . '" type="text/css" media="screen"/>';
}
/**
* Compiles given skin object
*
- * @param kDBItem $object
+ * @param kDBItem $object Skin.
+ *
+ * @return integer
*/
- function compile(&$object)
+ public function compile(kDBItem $object)
{
$ret = $object->GetDBField('CSS');
$options = $object->GetDBField('Options');
$options = unserialize($options);
- $options['base_url'] = Array ('Value' => rtrim(BASE_PATH, '/'));
+ $options['base_url'] = array('Value' => rtrim(BASE_PATH, '/'));
- foreach ($options as $key => $row) {
+ foreach ( $options as $key => $row ) {
$ret = str_replace('@@' . $key . '@@', $row['Value'], $ret);
}
$compile_ts = adodb_mktime();
- $css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $compile_ts . '.css';
+ $style_name = $this->getSkinFilename($object->GetDBField('Name'));
+ $css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . $style_name;
- $fp = fopen($css_file, 'w');
- if (!$fp) {
+ if ( file_put_contents($css_file, $ret) === false ) {
return false;
}
- $prev_css = $this->_getStylesheetPath() . '/admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $object->GetDBField('LastCompiled') . '.css';
- if (file_exists($prev_css)) {
- unlink($prev_css);
- }
-
- fwrite($fp, $ret);
- fclose($fp);
-
$sql = 'UPDATE ' . $object->TableName . '
SET LastCompiled = ' . $compile_ts . '
WHERE ' . $object->IDField . ' = ' . $object->GetID();
@@ -112,15 +91,14 @@
/**
* Returns fields of primary admin skin
*
- * @return Array
- * @access protected
+ * @return array
*/
protected function _getStyleInfo()
{
$cache_key = 'primary_skin_info[%SkinSerial%]';
$ret = $this->Application->getCache($cache_key);
- if ($ret === false) {
+ if ( $ret === false ) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'AdminSkins
@@ -136,24 +114,26 @@
/**
* Returns requested field value of primary admin skin
*
- * @param string $field
+ * @param string $field Field.
+ *
* @return string
*/
- function _getStyleField($field)
+ protected function _getStyleField($field)
{
- if ($field == 'logo') {
- // old style method of calling
+ // Old style method of calling.
+ if ( $field == 'logo' ) {
$field = 'Logo';
}
$style_info = $this->_getStyleInfo();
- if (!$style_info[$field]) {
+ if ( !$style_info[$field] ) {
return '';
}
- $image_fields = Array ('Logo', 'LogoBottom', 'LogoLogin');
- if (in_array($field, $image_fields)) {
+ $image_fields = array('Logo', 'LogoBottom', 'LogoLogin');
+
+ if ( in_array($field, $image_fields) ) {
return $this->_getStylesheetPath(true) . '/' . $style_info[$field];
}
@@ -163,14 +143,16 @@
/**
* Returns path, where compiled skin and it's image files are stored
*
- * @param bool $url
+ * @param boolean $url Return url or path.
+ *
* @return string
- * @access protected
*/
protected function _getStylesheetPath($url = false)
{
- if ($url) {
- return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) ) . 'user_files';
+ if ( $url ) {
+ $sub_folder = str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) . '/user_files';
+
+ return $this->Application->BaseURL() . ltrim($sub_folder, '/');
}
return WRITEABLE . DIRECTORY_SEPARATOR . 'user_files';
@@ -179,81 +161,42 @@
/**
* Returns full path to primary admin skin using given or last compiled date
*
- * @param string|bool $url
- * @param int $compile_date
+ * @param boolean $url Return url or path..
+ *
* @return string
- * @access public
*/
- public function getSkinPath($url = false, $compile_date = null)
+ public function getSkinPath($url = false)
{
$style_info = $this->_getStyleInfo();
-
- if ( !isset($compile_date) ) {
- $compile_date = $style_info['LastCompiled'];
- }
-
- $style_name = 'admin-' . mb_strtolower($style_info['Name']) . '-' . $compile_date . '.css';
+ $style_name = $this->getSkinFilename($style_info['Name']);
return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name;
}
/**
- * Returns maximal compilation date for given skin name
+ * Returns skin filename without path.
+ *
+ * @param string $skin_name Skin name.
*
- * @param string $style_name
- * @return int
+ * @return string
*/
- function _getLastCompiled($style_name)
+ protected function getSkinFilename($skin_name)
{
- $last_compiled = 0;
-
- $iterator = new DirectoryIterator( $this->_getStylesheetPath() . DIRECTORY_SEPARATOR );
- /** @var DirectoryIterator $file_info */
-
- foreach ($iterator as $file_info) {
- if ( !$file_info->isFile() ) {
- continue;
- }
-
- $regs = $this->isSkinFile( $file_info->getFilename() );
-
- if ( $regs && $regs[1] == $style_name && $regs[2] > $last_compiled ) {
- $last_compiled = max($last_compiled, $regs[2]);
- }
- }
-
- return $last_compiled;
+ return 'admin-' . mb_strtolower($skin_name) . '.css';
}
/**
* Deletes all compiled versions of all skins
*
+ * @return void
*/
- function deleteCompiled()
+ public function deleteCompiled()
{
- $iterator = new DirectoryIterator( $this->_getStylesheetPath() . DIRECTORY_SEPARATOR );
- /** @var DirectoryIterator $file_info */
+ $files = glob($this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-*.css');
- foreach ($iterator as $file_info) {
- if ( $file_info->isFile() && $this->isSkinFile( $file_info->getFilename() ) ) {
- unlink( $file_info->getPathname() );
- }
+ if ( $files ) {
+ array_map('unlink', $files);
}
}
- /**
- * Determines if given file is admin skin file
- *
- * @param string $filename
- * @return array|bool
- * @access protected
- */
- protected function isSkinFile($filename)
- {
- if ( preg_match(self::SKIN_REGEXP, $filename, $regs) ) {
- return $regs;
- }
-
- return false;
- }
- }
\ No newline at end of file
+ }
Event Timeline
Log In to Comment