Index: branches/5.2.x/core/units/helpers/skin_helper.php =================================================================== diff -u -N -r16513 -r16568 --- branches/5.2.x/core/units/helpers/skin_helper.php (.../skin_helper.php) (revision 16513) +++ branches/5.2.x/core/units/helpers/skin_helper.php (.../skin_helper.php) (revision 16568) @@ -1,6 +1,6 @@ _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)); + /** @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) : ''; - } + $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 ''; } /** * 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(); + $style_name = $this->getSkinFilename($style_info['Name']); - if ( !isset($compile_date) ) { - $compile_date = $style_info['LastCompiled']; - } - - $style_name = 'admin-' . mb_strtolower($style_info['Name']) . '-' . $compile_date . '.css'; - return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name; } /** - * Returns maximal compilation date for given skin name + * Returns skin filename without path. * - * @param string $style_name - * @return int + * @param string $skin_name Skin name. + * + * @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 + }