Cheetah
functions.inc.php
Go to the documentation of this file.
1 <?php
2 
9 {
10  return preg_match("/^([0-9A-Za-z_]+)$/i", $sWidget);
11 }
12 
18 function getError($sError)
19 {
20  $iNumArgs = func_num_args();
21 
22  for($i=1; $i<$iNumArgs; $i++) {
23  $sValue = func_get_arg($i);
24  $sError = str_replace("#" . $i. "#", $sValue, $sError);
25  }
26  return $sError;
27 }
28 
34 function checkPermissions($sFileName)
35 {
36  $sPermissions = "";
37  $sFilePath = trim($sFileName);
38  clearstatcache();
39  if(!file_exists($sFilePath))
40  $sResult = "";
41  else {
42  clearstatcache();
43  $iPermissions = fileperms($sFilePath);
44  for($i=0, $offset = 0; $i<3; $i++, $offset += 3) {
45  $iPerm = 0;
46  for($j=0; $j<3; $j++) ($iPermissions >> ($j+$offset)) & 1 ? $iPerm += pow(2, $j) : "";
47  $sPermissions = $iPerm . $sPermissions;
48  }
49  $sResult = $sPermissions;
50  }
51 
52  $sResult = "";
53  $bDir = is_dir($sFilePath);
54  if(is_readable($sFilePath)) $sResult = $bDir ? "755" : "644";
55  if(is_writable($sFilePath)) $sResult = $bDir ? "777" : "666";
56  if(!$bDir && is_executable($sFilePath)) $sResult = "777";
57 
58  return $sResult;
59 }
60 
66 {
69 
70  //--- Add info in the database ---//
71  $sWidgetFile = $sWidget . "/install/install.sql";
72  $sFileName = $sModulesPath . $sWidgetFile;
73  $sModuleDBPrefix = DB_PREFIX . strtoupper(substr($sWidget, 0, 1)) . substr($sWidget, 1);
74 
75  if(!file_exists($sFileName)) return getError($aErrorCodes[1], $sWidgetFile);
76  $rHandler = fopen($sFileName, "r");
77  while(!feof($rHandler)) {
78  $str = fgets($rHandler);
79  if($str[0]=="" || $str[0]=="#" || ($str[0] == "-" && $str[1] == "-")) continue;
80  $str = str_replace("[module_db_prefix]", $sModuleDBPrefix, $str);
81  if( (strlen($str) > 5 ? strpos($str, ";", strlen($str) - 4) : strpos($str, ";")) )
82  $sQuery .= $str;
83  else {
84  $sQuery .= $str;
85  continue;
86  }
87  if(!($res = getResult($sQuery))) return $aErrorCodes[5];
88  $sQuery = "";
89  }
90  fclose($rHandler);
91  return "";
92 }
93 
99 {
104 
105  if(!isset($sWidget)) return;
106 
107  $sBegin = '//' . $sWidget . ' begin';
108  $sAppsArray = "\n" . 'aRayApps["' . $sWidget . '"] = new Array();' . "\n";
109  $sAppTmpl = 'aRayApps["' . $sWidget . '"]["#app#"] = {"params": new Array(#params#), "top": #top#, "left": #left#, "width": #width#, "height": #height#, "resizable": #resizable#};' . "\n";
110  $sEnd = '//' . $sWidget . ' end';
111 
112  //getting integrator contents
113  $sFile = $sGlobalDir . "data/integration.dat";
114  $sFileName = $sModulesPath . $sFile;
115  if(!file_exists($sFileName)) return parseXml($aXmlTemplates['result'], getError($aErrorCodes[1], $sFile), FAILED_VAL);
116  $rHandle = fopen($sFileName, "rt");
117  $sJSContents = fread($rHandle, filesize($sFileName)) ;
118  fclose($rHandle);
119 
120  //creating insert string
121  $sInsert = $sBegin . $sAppsArray;
122  $sApps = "";
123  foreach($aModules as $sAppName => $aModule) {
124  if($aModule['inline']) continue;
125 
126  $sApp = $sAppTmpl;
127  $sApp = str_replace("#app#", $sAppName, $sApp);
128  $sParams = "'" . implode("', '", $aModule['parameters']) . "'";
129  $sApp = str_replace("#params#", $sParams, $sApp);
130  $sApp = str_replace("#top#", $aModule['layout']['top'], $sApp);
131  $sApp = str_replace("#left#", $aModule['layout']['left'], $sApp);
132 
133  $iWidth = getSettingValue($sWidget, $sAppName . "_width");
134  if(empty($iWidth)) $iWidth = $aModule['layout']['width'];
135  if(!is_numeric($iWidth)) $iWidth = $aModule['minSize']['width'];
136  $iHeight = getSettingValue($sWidget, $sAppName . "_height");
137  if(empty($iHeight)) $iHeight = $aModule['layout']['height'];
138  if(!is_numeric($iHeight)) $iHeight = $aModule['minSize']['height'];
139 
140  $sApp = str_replace("#width#", $iWidth, $sApp);
141  $sApp = str_replace("#height#", $iHeight, $sApp);
142  $sResizable = ($aModule['vResizable'] || $aModule['hResizable']) ? "1" : "0";
143  $sApp = str_replace("#resizable#", $sResizable, $sApp);
144  $sApps .= $sApp;
145  }
146  if(empty($sApps)) return array('value' => "", 'status' => SUCCESS_VAL);
147 
148  $sInsert .= $sApps . $sEnd;
149 
150  //inserting javascript code
151  $iInsertBegin = strpos($sJSContents, $sBegin);
152  $iInsertEnd = strpos($sJSContents, $sEnd) + strlen($sEnd);
153  if($iInsertBegin === false) $sJSContents .= $sInsert . '\n';
154  else $sJSContents = substr($sJSContents, 0, $iInsertBegin) . $sInsert . substr($sJSContents, $iInsertEnd);
155 
156  //--- Save changes to the file---//
157  $bResult = true;
158  if(($rHandle = @fopen($sFileName, "wt")) !== false) {
159  $bResult = (fwrite($rHandle, $sJSContents) !== false);
160  fclose($rHandle);
161  }
162  $sValue = $bResult && $rHandle ? "" : getError($aErrorCodes[2], $sFile);
163 
164  return array('value' => $sValue, 'status' => $bResult ? SUCCESS_VAL : FAILED_VAL);
165 }
166 
172 function refreshExtraFile($sWidget, $sCase, $bReset = false, $sDefaultFile = "", $aEnabledFiles = array())
173 {
177 
178  //--- Get folder contents ---//
179  $sDir = $sWidget . "/" . $sCase;
180  $sDirName = $sModulesPath . $sDir;
181  if( !(file_exists($sDirName) && is_dir($sDirName)) ) return parseXml($aXmlTemplates['result'], getError($aErrorCodes[6], $sDir), FAILED_VAL);
182 
183  $aFiles = getExtraFiles($sWidget, $sCase, false);
184  $iFilesCount = count($aFiles['files']);
185  if($iFilesCount == 0) return array('value' => getError($aErrorCodes[7], $sDir), 'status' => FAILED_VAL, 'contents' => "");
186 
187  //--- Get XML file contents ---//
188  $aFileContents = getFileContents($sWidget, "/xml/" . $sCase . ".xml", true);
189  if($aFileContents['status'] == FAILED_VAL) return array('value' => $aFileContents['value'], 'status' => FAILED_VAL, 'contents' => "");
190  $aContents = $aFileContents['contents'];
191 
192  //--- Merge folder and file contents ---//
193  $sCurrent = isset($aContents[FILE_DEFAULT_KEY]) && in_array($aContents[FILE_DEFAULT_KEY], $aFiles["files"]) ? $aContents[FILE_DEFAULT_KEY] : $aFiles["current"];
194  $sCurrent = $bReset && in_array($sDefaultFile, $aFiles["files"]) ? $sDefaultFile : $sCurrent;
195  $aEnabledFiles[] = $sCurrent;
196  $sContents = parseXml($aXmlTemplates["item"], FILE_DEFAULT_KEY, $sCurrent);
197 
198  for($i=0; $i<$iFilesCount; $i++) {
199  $sEnabled = isset($aContents[$aFiles["files"][$i]]) ? $aContents[$aFiles["files"][$i]] : TRUE_VAL;
200  if($bReset)
201  $sEnabled = in_array($aFiles["files"][$i], $aEnabledFiles) ? TRUE_VAL : FALSE_VAL;
202  $sContents .= parseXml($aXmlTemplates["item"], $aFiles["files"][$i], $sEnabled);
203  }
204  $sContents = makeGroup($sContents, "items");
205 
206  //--- Save changes to the file---//
207  $sFile = $sWidget . "/xml/" . $sCase . ".xml";
208  $sFileName = $sModulesPath . $sFile;
209  $bResult = false;
210  if(($rHandle = @fopen($sFileName, "wt")) !== false) {
211  $bResult = (fwrite($rHandle, $sContents) !== false);
212  fclose($rHandle);
213  }
214  $bResult = $bResult && $rHandle;
215  $sValue = $bResult ? "" : getError($aErrorCodes[2], $sFile);
216 
217  return array('value' => $sValue, 'status' => $bResult ? SUCCESS_VAL : FAILED_VAL, 'contents' => $sContents);
218 }
219 
225 {
229 
230  $bResult = false;
232  require($sModulesPath . $sWidget . "/inc/constants.inc.php");
233 
234  $sCode = $aInfo['code'];
235  $sContents = parseXml($aXmlTemplates["item"], "status", WIDGET_STATUS_NOT_REGISTERED);
236  $sContents .= parseXml($aXmlTemplates["item"], "license", "");
237  $sContents .= parseXml($aXmlTemplates["item"], "code", $sCode);
238  $sContents .= parseXml($aXmlTemplates["item"], "updated", TRUE_VAL);
239  $sContents .= parseXml($aXmlTemplates["item"], "updateLast", "");
240  $sContents .= parseXml($aXmlTemplates["item"], "updateUrl", "");
241  $sContents = makeGroup($sContents, "items");
242 
243  //--- Save changes to the file---//
244  $sFile = $sWidget . "/xml/main.xml";
245  $sFileName = $sModulesPath . $sFile;
246  if(($rHandle = @fopen($sFileName, "wt")) !== false) {
247  $bResult = (fwrite($rHandle, $sContents) !== false);
248  fclose($rHandle);
249  }
250  $bResult = $bResult && $rHandle;
251  $sValue = $bResult ? "" : getError($aErrorCodes[2], $sFile);
252  } else {
253  $sValue = $aErrorCodes[8];
254  }
255 
256  return array('value' => $sValue, 'status' => $bResult ? SUCCESS_VAL : FAILED_VAL);
257 }
258 
264 function getFileContents($sWidget, $sFile, $bArray = false)
265 {
268 
269  $sFile = $sWidget . $sFile;
270  $sFileName = $sModulesPath . $sFile;
271  if(!file_exists($sFileName)) return array('value' => getError($aErrorCodes[1], $sFile), 'status' => FAILED_VAL, 'contents' => $bArray ? array() : "");
272  $rHandle = fopen($sFileName, "rt");
273  $iFileSize = filesize($sFileName);
274  $sContents = $iFileSize > 0 ? fread($rHandle, filesize($sFileName)) : makeGroup("", "items");
275  fclose($rHandle);
276 
277  $aContents = xmlGetValues($sContents, "item");
278  return array('value' => "", 'status' => SUCCESS_VAL, 'contents' => $bArray ? $aContents : $sContents);
279 }
280 
287 {
289 
290  $aRightExtensions = array("swf", "xml");
291  $sFolderPath = $sModulesPath . $sWidget . "/" . $sFolder . "/";
292  if($rDirHandle = opendir($sFolderPath))
293  while (false !== ($sFile = readdir($rDirHandle))) {
294  $aPathInfo = pathinfo($sFolderPath . $sFile);
295  if(is_file($sFolderPath . $sFile) && $sFile != "." && $sFile != ".." && in_array($aPathInfo['extension'], $aRightExtensions))
296  return $aPathInfo['extension'];
297  }
298  return "";
299 }
300 
301 function smartReadFile($sPath, $sFilename = '', $sMimeType = 'application/octet-stream', $iCacheAge = 0, $sCachePrivacy = 'public')
302 {
303  if (!file_exists($sPath)) {
304  header ("HTTP/1.0 404 Not Found");
305  return false;
306  }
307 
308  $fp = @fopen($sPath, 'rb');
309 
310  $size = filesize($sPath);
311  $length = $size;
312  $start = 0;
313  $end = $size - 1;
314 
315  header('Content-type: ' . $sMimeType);
316  header('Cache-Control: ' . $sCachePrivacy . ', must-revalidate, max-age=' . $iCacheAge);
317  header("Accept-Ranges: 0-$length");
318  if ($sFilename)
319  header('Content-Disposition: inline; filename=' . $sFilename);
320 
321  if (isset($_SERVER['HTTP_RANGE'])) {
322 
323  $c_start = $start;
324  $c_end = $end;
325 
326  list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
327  if (strpos($range, ',') !== false) {
328  header('HTTP/1.1 416 Requested Range Not Satisfiable');
329  header("Content-Range: bytes $start-$end/$size");
330  return false;
331  }
332  if ($range == '-') {
333  $c_start = $size - substr($range, 1);
334  }else{
335  $range = explode('-', $range);
336  $c_start = $range[0];
337  $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
338  }
339  $c_end = ($c_end > $end) ? $end : $c_end;
340  if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
341  header('HTTP/1.1 416 Requested Range Not Satisfiable');
342  header("Content-Range: bytes $start-$end/$size");
343  return false;
344  }
345  $start = $c_start;
346  $end = $c_end;
347  $length = $end - $start + 1;
348  fseek($fp, $start);
349  header('HTTP/1.1 206 Partial Content');
350  }
351  header("Content-Range: bytes $start-$end/$size");
352  header("Content-Length: ".$length);
353 
354 
355  $buffer = 1024 * 8;
356  while(!feof($fp) && ($p = ftell($fp)) <= $end) {
357 
358  if ($p + $buffer > $end) {
359  $buffer = $end - $p + 1;
360  }
361  set_time_limit(0);
362  echo fread($fp, $buffer);
363  flush();
364  }
365 
366  fclose($fp);
367 
368  return true;
369 }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
getFileExtension
getFileExtension($sWidget, $sFolder)
Definition: functions.inc.php:286
$aPathInfo
$aPathInfo
Definition: cmd.php:12
getFileContents
getFileContents($sWidget, $sFile, $bArray=false)
Definition: functions.inc.php:264
$sCode
$sCode
Definition: explanation.php:19
$sResult
$sResult
Definition: advanced_settings.php:26
$aModule
$aModule
Definition: classifieds.php:21
refreshExtraFile
refreshExtraFile($sWidget, $sCase, $bReset=false, $sDefaultFile="", $aEnabledFiles=array())
Definition: functions.inc.php:172
php
getExtraFiles
getExtraFiles($sModule, $sFolder="langs", $bGetUserFile=true, $bGetDate=false)
Definition: apiFunctions.inc.php:168
getError
getError($sError)
Definition: functions.inc.php:18
$sModulesPath
$sModulesPath
Definition: header.inc.php:53
xmlGetValues
xmlGetValues($sXmlContent, $sXmlTagName)
Definition: xml.inc.php:199
createDataBase
createDataBase($sWidget)
Definition: functions.inc.php:65
$sWidget
$sWidget
Definition: actions.inc.php:18
$aInfo
$aInfo
Definition: constants.inc.php:21
secureCheckWidgetName
secureCheckWidgetName($sWidget)
Definition: functions.inc.php:8
recompileIntegrator
recompileIntegrator($sWidget)
Definition: functions.inc.php:98
$sFile
$sFile
Definition: index.php:20
checkPermissions
checkPermissions($sFileName)
Definition: functions.inc.php:34
$sGlobalDir
$sGlobalDir
Definition: header.inc.php:55
$sFolder
$sFolder
Definition: index.php:15
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
$bResult
$bResult
Definition: get_file.php:11
$sDefaultFile
$sDefaultFile
Definition: actions.inc.php:24
$aErrorCodes
$aErrorCodes
Definition: constants.inc.php:26
smartReadFile
smartReadFile($sPath, $sFilename='', $sMimeType='application/octet-stream', $iCacheAge=0, $sCachePrivacy='public')
Definition: functions.inc.php:301
parseXml
parseXml($aXmlTemplates)
Definition: apiFunctions.inc.php:15
$aModules
$aModules
Definition: constants.inc.php:29
makeGroup
makeGroup($sXmlContent, $sXmlGroup="ray")
Definition: apiFunctions.inc.php:32
$aXmlTemplates
$aXmlTemplates
Definition: xmlTemplates.inc.php:8
getSettingValue
getSettingValue($sWidget, $sSettingKey, $sFile="config", $bFullReturn=false, $sFolder="xml")
Definition: apiFunctions.inc.php:82
$sContents
$sContents
Definition: XML.php:38
createMainFile
createMainFile($sWidget)
Definition: functions.inc.php:224
$sError
$sError
Definition: index.php:425
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$sApp
$sApp
Definition: index.php:15
getResult
getResult($sQuery)
Definition: db.inc.php:45