public function PrepareFile($sFile, $sDestination) { $sContents = F::File_GetContents($sFile); if ($sContents !== false) { $sContents = $this->PrepareContents($sContents, $sFile, $sDestination); if (F::File_PutContents($sDestination, $sContents) !== false) { return $sDestination; } } F::SysWarning('Can not prepare asset file "' . $sFile . '"'); }
public function __get($sName) { // LS compatibility if ($sName === 'oEngine') { $this->oEngine = E::getInstance(); return $this->oEngine; } $trace = debug_backtrace(); $sError = 'Undefined property via __get(): ' . $sName . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line']; F::SysWarning($sError); return null; }
/** * Plugin for Smarty * Eval widget groups * * @param array $aParams * @param Smarty_Internal_Template $oSmartyTemplate * * @return string */ function smarty_function_wgroup($aParams, $oSmartyTemplate) { if (empty($aParams['group']) && empty($aParams['name'])) { $sError = 'Parameter "group" does not define in {wgroup ...} function'; if ($oSmartyTemplate->template_resource) { $sError .= ' (template: ' . $oSmartyTemplate->template_resource . ')'; } F::SysWarning($sError); return null; } if (empty($aParams['group']) && !empty($aParams['name'])) { $aParams['group'] = $aParams['name']; unset($aParams['name']); } $sWidgetGroup = $aParams['group']; $aWidgetParams = isset($aParams['params']) ? array_merge($aParams['params'], $aParams) : $aParams; // group parameter required if (!$sWidgetGroup) { return null; } if (isset($aParams['command'])) { $sWidgetCommand = $aParams['command']; } else { $sWidgetCommand = 'show'; } if ($sWidgetCommand == 'show') { if (!function_exists('smarty_function_wgroup_show')) { F::IncludeFile('function.wgroup_show.php'); } unset($aWidgetParams['group']); if (isset($aWidgetParams['command'])) { unset($aWidgetParams['command']); } return smarty_function_wgroup_show(array('group' => $sWidgetGroup, 'params' => $aWidgetParams), $oSmartyTemplate); } elseif ($sWidgetCommand == 'add') { if (!isset($aWidgetParams['widget'])) { F::SysWarning('Parameter "widget" does not define in {wgroup ...} function'); return null; } if (!function_exists('smarty_function_wgroup_add')) { F::IncludeFile('function.wgroup_add.php'); } $sWidgetName = $aWidgetParams['widget']; unset($aWidgetParams['group']); unset($aWidgetParams['widget']); if (isset($aWidgetParams['command'])) { unset($aWidgetParams['command']); } return smarty_function_wgroup_add(array('group' => $sWidgetGroup, 'widget' => $sWidgetName, 'params' => $aWidgetParams), $oSmartyTemplate); } return ''; }
/** * Plugin for Smarty * Adds widget into widget group * * @param array $aParams * @param Smarty_Internal_Template $oSmartyTemplate * * @return string */ function smarty_function_wgroup_add($aParams, $oSmartyTemplate) { if (isset($aParams['name'])) { if (!isset($aParams['group'])) { $aParams['group'] = $aParams['name']; } elseif (!isset($aParams['widget'])) { $aParams['widget'] = $aParams['name']; } } if (!isset($aParams['group']) && !isset($aParams['name'])) { $sError = 'Parameter "group" does not define in {wgroup_add ...} function'; if ($oSmartyTemplate->template_resource) { $sError .= ' (template: ' . $oSmartyTemplate->template_resource . ')'; } F::SysWarning($sError); return null; } if (!isset($aParams['widget'])) { $sError = 'Parameter "widget" does not define in {wgroup_add ...} function'; if ($oSmartyTemplate->template_resource) { $sError .= ' (template: ' . $oSmartyTemplate->template_resource . ')'; } F::SysWarning($sError); return null; } $aWidgetParams = isset($aParams['params']) ? (array) $aParams['params'] : array(); if (array_key_exists('priority', $aWidgetParams)) { $nPriority = $aWidgetParams['priority']; } elseif (array_key_exists('priority', $aParams)) { $nPriority = $aParams['priority']; } else { $nPriority = 0; } foreach ($aParams as $sKey => $sVal) { if (!in_array($sKey, array('group', 'name', 'widget', 'params', 'priority'))) { $aWidgetParams[$sKey] = $sVal; } } E::ModuleViewer()->AddWidget($aParams['group'], $aParams['widget'], $aWidgetParams, $nPriority); $aWidgets = E::ModuleViewer()->GetWidgets(); $oSmartyTemplate->assign('aWidgets', $aWidgets); $oSmartyTemplate->parent->assign('aWidgets', $aWidgets); return ''; }
/** * Plugin for Smarty * Display widget group * * @param array $aParams * @param Smarty_Internal_Template $oSmartyTemplate * * @return string */ function smarty_function_wgroup_show($aParams, $oSmartyTemplate) { static $aStack = array(); if (empty($aParams['group']) && empty($aParams['name'])) { $sError = 'Parameter "group" does not define in {wgroup_show ...} function'; if ($oSmartyTemplate->template_resource) { $sError .= ' (template: ' . $oSmartyTemplate->template_resource . ')'; } F::SysWarning($sError); return null; } if (empty($aParams['group']) && !empty($aParams['name'])) { $aParams['group'] = $aParams['name']; unset($aParams['name']); } $sWidgetGroup = $aParams['group']; $aWidgetParams = isset($aParams['params']) ? array_merge($aParams['params'], $aParams) : $aParams; if (isset($aStack[$sWidgetGroup])) { // wgroup nested in self $sError = 'Template function {wgroup group="' . $sWidgetGroup . '" nested in self '; if ($oSmartyTemplate->template_resource) { $sError .= ' (template: ' . $oSmartyTemplate->template_resource . ')'; } F::SysWarning($sError); return null; } // add group into the stack $aStack[$sWidgetGroup] = $aWidgetParams; $aWidgets = E::ModuleViewer()->GetWidgets(); $sResult = ''; if (isset($aWidgets[$sWidgetGroup])) { if (!function_exists('smarty_function_widget')) { F::IncludeFile('function.widget.php'); } foreach ($aWidgets[$sWidgetGroup] as $oWidget) { $sResult .= smarty_function_widget(array_merge($aWidgetParams, array('widget' => $oWidget)), $oSmartyTemplate); } } // Pop element off the stack array_pop($aStack); return $sResult; }
/** * Подключение файла * * @param string $sFile * @param bool $bOnce * @param mixed $xConfig * * @return mixed */ public static function IncludeFile($sFile, $bOnce = true, $xConfig = false) { if (is_array($xConfig)) { $config = $xConfig; } else { $config = array(); } try { self::$_time = microtime(true); if (F::IsDebug()) { $bCheckUtf8 = class_exists('Config', false) ? Config::Get('sys.include.check_file') : false; if ($bCheckUtf8) { $sBom = file_get_contents($sFile, true, null, 0, 5); if (!$sBom) { F::SysWarning('Error in including file "' . $sFile . '" - file is empty'); } elseif ($sBom != '<?php') { F::SysWarning('Error in including file "' . $sFile . '" - BOM or other wrong symbols detected'); } } } if ($bOnce) { self::$_temp = (include_once $sFile); } else { self::$_temp = (include $sFile); } $nTime = microtime(true) - self::$_time; self::$nIncludedTime += $nTime; self::$nIncludedCount++; if (F::IsDebug()) { self::$aIncludedFiles[] = $sFile . '; result: ' . (is_scalar(self::$_temp) ? self::$_temp : gettype(self::$_temp)) . '; time: ' . $nTime; } } catch (ErrorException $oException) { if ($oException->getFile() !== __FILE__) { // Ошибка в подключённом файле //throw $oException; F::SysWarning('Error in including file "' . $sFile . '" - ' . $oException->getMessage()); return false; } else { // Файл не был подключён. F::SysWarning('Can not include file "' . $sFile . '"'); return false; } } if ($xConfig !== false && !is_array(self::$_temp) && is_array($config)) { self::$_temp = $config; } return self::$_temp; }
/** * @param string $sType * @param array|string $aFiles * @param array $aOptions */ protected function _add($sType, $aFiles, $aOptions = array()) { if ($oAssetPackage = $this->_getAssetPackage($sType)) { $aAddFiles = array(); foreach ($aFiles as $sFileName => $aFileParams) { // extract & normalize full file path if (isset($aFileParams['file'])) { $sFilePath = F::File_NormPath($aFileParams['file']); } else { $sFilePath = F::File_NormPath((string) $sFileName); } // if file path defined if ($sFilePath) { if (!is_array($aFileParams)) { $aFileParams = array('file' => $sFilePath); } else { $aFileParams['file'] = $sFilePath; } if (!isset($aFileParams['name'])) { $aFileParams['name'] = $aFileParams['file']; } $aAddFiles[$aFileParams['name']] = $aFileParams; } else { F::SysWarning('Can not define asset file path "' . $sFilePath . '"'); } } if ($aAddFiles) { $oAssetPackage->AddFiles($aAddFiles, null, isset($aOptions['prepend']) ? $aOptions['prepend'] : false, isset($aOptions['replace']) ? $aOptions['replace'] : null); } } }
/** * AJAX подгрузка следующих фото * */ protected function EventAjaxPhotoGetMore() { // * Устанавливаем формат Ajax ответа E::ModuleViewer()->SetResponseAjax('json'); // * Существует ли топик $iTopicId = F::GetRequestStr('topic_id'); $iLastId = F::GetRequest('last_id'); $sThumbSize = F::GetRequest('thumb_size'); if (!$sThumbSize) { $sThumbSize = '50crop'; } if (!$iTopicId || !($oTopic = E::ModuleTopic()->GetTopicById($iTopicId)) || !$iLastId) { E::ModuleMessage()->AddError(E::ModuleLang()->Get('system_error'), E::ModuleLang()->Get('error')); F::SysWarning('System Error'); return; } // * Получаем список фото $aPhotos = $oTopic->getPhotosetPhotos($iLastId, Config::Get('module.topic.photoset.per_page')); $aResult = array(); if (count($aPhotos)) { // * Формируем данные для ajax ответа foreach ($aPhotos as $oPhoto) { $aResult[] = array('id' => $oPhoto->getMresourceId(), 'path_thumb' => $oPhoto->getLink($sThumbSize), 'path' => $oPhoto->getLink(), 'description' => $oPhoto->getDescription()); } E::ModuleViewer()->AssignAjax('photos', $aResult); } E::ModuleViewer()->AssignAjax('bHaveNext', count($aPhotos) == Config::Get('module.topic.photoset.per_page')); }
/** * При завершении работы модуля пишем ошибки в лог, если они есть */ public function Shutdown() { while ($sError = $this->GetError(true)) { F::SysWarning($sError); } }
/** * Создание ресурса из множества файлов * * @param string $sAsset * @param array $aFiles * * @return bool */ public function MakeMerge($sAsset, $aFiles) { $sDestination = F::File_GetAssetDir() . md5($sAsset . serialize($aFiles)) . '.' . $this->sOutType; if (!$this->CheckDestination($sDestination)) { $sContents = ''; $bCompress = $this->bCompress; $bPrepare = null; foreach ($aFiles as $aFileParams) { $sFileContents = trim(F::File_GetContents($aFileParams['file'])); $sContents .= $this->PrepareContents($sFileContents, $aFileParams['file']) . PHP_EOL; if (isset($aFileParams['compress'])) { $bCompress = $bCompress && (bool) $aFileParams['compress']; } // Если хотя бы один файл из набора нужно выводить, то весь набор выводится if ((is_null($bPrepare) || $bPrepare === true) && isset($aFileParams['prepare']) && !$aFileParams['prepare']) { $bPrepare = false; } } if (F::File_PutContents($sDestination, $sContents)) { $aParams = array('file' => $sDestination, 'asset' => $sAsset, 'asset_file' => $sDestination, 'compress' => $bCompress, 'prepare' => is_null($bPrepare) ? false : $bPrepare); $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams); } else { F::SysWarning('Can not write asset file "' . $sDestination . '"'); return false; } } else { $aParams = array('file' => $sDestination, 'asset' => $sAsset, 'asset_file' => $sDestination, 'compress' => $this->bCompress, 'prepare' => false); $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams); } return true; }
/** * Writes message to log file * * @param string $sMsg - message to log * * @return bool|int */ protected function _write($sMsg) { $xResult = false; // if no filename then nothing to do if (!($sFileName = $this->GetFileName())) { //throw new Exception("Empty file name for log!"); return false; } // if filename equal '-' then wtites message to browser if ($sFileName == '-') { echo $sMsg . "<br/>\n"; } else { // writes to file $sFile = $this->GetFileDir() . $sFileName; // file for locking $sCheckFileName = $sFile . '.lock'; if (is_file($sCheckFileName) && !is_writeable($sCheckFileName)) { F::SysWarning('Cannot write to file ' . $sCheckFileName); } else { $fp = @fopen($sCheckFileName, 'c'); if (!$fp) { // It is not clear what to do here if ($xResult = F::File_PutContents($sFile, $sMsg . "\n", FILE_APPEND | LOCK_EX)) { // Do rotation if need if ($this->GetUseRotate() && $this->GetSizeForRotate()) { $this->_rotate(); } } } else { // Tries to lock $iTotal = 0; // Check the count of attempts at competitive lock requests for ($iCnt = 0; $iCnt < self::MAX_LOCK_CNT; $iCnt++) { if (flock($fp, LOCK_EX)) { if ($xResult = F::File_PutContents($sFile, $sMsg . "\n", FILE_APPEND | LOCK_EX)) { // Do rotation if need if ($this->GetUseRotate() && $this->GetSizeForRotate()) { $this->_rotate(); } } flock($fp, LOCK_UN); break; } else { $iTotal += self::MAX_LOCK_PERIOD; if ($iTotal >= self::MAX_LOCK_TIME) { break; } usleep(self::MAX_LOCK_PERIOD); } } fclose($fp); } } } return $xResult; }
/** * Sets cookie * * @param string $sName * @param string $sValue * @param int|string|null $xPeriod - period in seconds or in string like 'P<..>' * @param bool $bHttpOnly * @param bool $bSecure * * @return bool */ public function SetCookie($sName, $sValue, $xPeriod = null, $bHttpOnly = true, $bSecure = false) { if ($xPeriod) { $nTime = time() + F::ToSeconds($xPeriod); } else { $nTime = 0; } $bResult = setcookie($sName, $sValue, $nTime, Config::Get('sys.cookie.path'), Config::Get('sys.cookie.host'), $bSecure, $bHttpOnly); if (DEBUG) { if (!$bResult) { if (headers_sent($sFilename, $iLine)) { F::SysWarning('Cannot set cookie "' . $sName . '" - header was sent in file ' . $sFilename . '(' . $iLine . ')'); } else { F::SysWarning('Cannot set cookie "' . $sName . '"'); } } } return $bResult; }
/** * Создание ресурса из одиночного файла * * @param string $sAsset * @param array $aFileParams * * @return bool */ public function MakeSingle($sAsset, $aFileParams) { $sFile = $aFileParams['file']; if (isset($aFileParams['dir_from'])) { $sLocalPath = F::File_LocalPath(dirname($sFile), $aFileParams['dir_from']); $sDir = $aFileParams['dir_from']; } else { $sLocalPath = ''; $sDir = dirname($sFile); } if ($aFileParams['merge']) { $sSubdir = $this->_makeSubdir($sAsset . $sDir); } else { $sSubdir = $this->_makeSubdir($sDir); } if ($sLocalPath) { $sSubdir .= '/' . $sLocalPath; } $sDestination = F::File_GetAssetDir() . $sSubdir . '/' . basename($sFile); $aFileParams['asset_file'] = $sDestination; if (!$this->CheckDestination($sDestination)) { if ($sDestination = $this->PrepareFile($sFile, $sDestination)) { $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams); } else { F::SysWarning('Can not prepare asset file "' . $sFile . '"'); return false; } } else { $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams); } return true; }
/** * Подключение к базе данных * @param string $dsn DSN строка БД */ protected function connect($dsn) { $parsed = $this->parseDSN($dsn); if (!$parsed) { $this->errorHandler('Parsing error of DSN', $dsn); } if (!isset($parsed['scheme'])) { $this->errorHandler('Database driver not define', $parsed); } $this->shema = ucfirst($parsed['scheme']); require_once __DIR__ . '/Driver/' . $this->shema . '.php'; $class = 'DbSimple_Driver_' . $this->shema; $nErrorFlag = error_reporting(); error_reporting($nErrorFlag & ~E_WARNING & ~E_USER_WARNING); $this->DbSimple = new $class($parsed); $this->errmsg =& $this->DbSimple->errmsg; $this->error =& $this->DbSimple->error; error_reporting($nErrorFlag); if (!$this->DbSimple || $this->error) { // Error of database initialization if ($this->DbSimple && $this->DbSimple->errmsg) { error_reporting($nErrorFlag & ~E_WARNING & ~E_USER_WARNING); F::SysWarning($this->DbSimple->errmsg); error_reporting($nErrorFlag); } die("<br><br>\n\n Cannot connect to database"); } else { $prefix = isset($parsed['prefix']) ? $parsed['prefix'] : ($this->_identPrefix ? $this->_identPrefix : false); if ($prefix) { $this->DbSimple->setIdentPrefix($prefix); } if ($this->_cachePrefix) { $this->DbSimple->setCachePrefix($this->_cachePrefix); } if ($this->_cacher) { $this->DbSimple->setCacher($this->_cacher); } if ($this->_logger) { $this->DbSimple->setLogger($this->_logger); } if ($this->_preformatter) { $this->DbSimple->setPreFormatter($this->_preformatter); } if ($this->_tablefunc) { $this->DbSimple->setTableNameFunc($this->_tablefunc); } $this->DbSimple->setErrorHandler($this->errorHandler !== null ? $this->errorHandler : array(&$this, 'errorHandler')); // Eval init queries if they are exist foreach ($this->init as $query) { call_user_func_array(array($this->DbSimple, 'query'), $query); } $this->init = array(); } }