PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_joomlapack/classes/core/utility/tables.php

https://github.com/Ratmir15/Joomla---formula-of-success
PHP | 299 lines | 174 code | 31 blank | 94 comment | 28 complexity | 38f4878757d2bc74f514a8825e8d1286 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JoomlaPack
  4. * @copyright Copyright (C) 2006-2009 JoomlaPack Developers. All rights reserved.
  5. * @version $Id$
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. * @since 1.2.1
  8. * @version 1.3
  9. *
  10. * JoomlaPack is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. **/
  15. // Ensure this file is being included by a parent file - Joomla! 1.0.x and 1.5 compatible
  16. (defined( '_VALID_MOS' ) || defined('_JEXEC')) or die( 'Direct Access to this location is not allowed.' );
  17. /**
  18. * Provides static functions to reading / writing data to & from JoomlaPack's database table
  19. * (the #__jp_temp table)
  20. */
  21. class JoomlapackCUBETables
  22. {
  23. /**
  24. * Writes a variable to the database (#__jp_temp)
  25. *
  26. * @param string $varName The name of the variable (must be unique; if it exists it gets overwritten)
  27. * @param string $value The value to store
  28. * @static
  29. */
  30. function WriteVar( $varName, $value ){
  31. $db =& JFactory::getDBO();
  32. // Hopefully fixes the 'MySQL server has gone away' errors by testing MySQL
  33. // connection status and reconnecting if necessary.
  34. //$db->connected();
  35. // Kill exisiting variable (if any)
  36. JoomlapackCUBETables::DeleteVar( $varName );
  37. // Base64-encode the value
  38. $value2 = JoomlapackCUBETables::_getBase64() ? base64_encode($value) : $value;
  39. // Create variable
  40. // OMG! $db-Quote DOES NOT encode 'key' into `key`, thus causing havoc. I have
  41. // to use backticks manually. BUMMER! AAARGH!
  42. $sql = 'INSERT INTO #__jp_temp (`key`,`value`)'.
  43. ' VALUES ('.$db->Quote( $varName ).', '.$db->Quote( $value2 ).')';
  44. $db->setQuery( $sql );
  45. if($db->query() === false)
  46. {
  47. JoomlapackLogger::WriteLog(_JP_LOG_ERROR,JText::sprintf('CUBE_TABLES_FAILEDSTORE', $varName));
  48. JoomlapackLogger::WriteLog(_JP_LOG_ERROR,'Error: '.$db->getErrorMsg());
  49. JoomlapackLogger::WriteLog(_JP_LOG_ERROR,'Value: '.$value);
  50. return false;
  51. }
  52. return true;
  53. }
  54. /**
  55. * Reads a variable out of #__jp_packvars
  56. *
  57. * @param string $key The name of the variable to read
  58. * @param bool $boolLongText True if you want to store a large string; deprecated since 1.1.2b
  59. * @return string
  60. * @static
  61. */
  62. function ReadVar( $key, $boolLongText = false ) {
  63. $db =& JFactory::getDBO();
  64. $sql = 'SELECT `value` FROM '.$db->nameQuote('#__jp_temp').
  65. ' WHERE `key` = '.$db->Quote($key);
  66. $db->setQuery( $sql );
  67. $db->query();
  68. $value2 = $db->loadResult();
  69. return JoomlapackCUBETables::_getBase64() ? base64_decode($value2) : $value;
  70. }
  71. /**
  72. * Removes a variable from #__jp_packvars
  73. *
  74. * @param string $varName The variable to remove
  75. * @static
  76. */
  77. function DeleteVar( $varName )
  78. {
  79. $db = JFactory::getDBO();
  80. $sql = 'DELETE FROM '.$db->nameQuote('#__jp_temp').' WHERE '.
  81. '`key` = '.$db->Quote($varName);
  82. $db->setQuery($sql);
  83. $db->query();
  84. if($varName == 'CUBEObject')
  85. {
  86. if( JoomlapackCUBETables::_isSetCUBEInFile())
  87. JoomlapackCUBETables::_deleteCUBEObject();
  88. }
  89. }
  90. /**
  91. * Removes all variables matching a pattern from #__jp_packvars
  92. *
  93. * @param string $keyPattern The name pattern the variables to be removed must follow
  94. * @static
  95. */
  96. function DeleteMultipleVars( $keyPattern )
  97. {
  98. $db = JFactory::getDBO();
  99. $sql = 'DELETE FROM '.$db->nameQuote('#__jp_temp').' WHERE '.
  100. '`key` LIKE '.$db->Quote($keyPattern);
  101. $db->setQuery($sql);
  102. $db->query();
  103. if($keyPattern == '%CUBE%')
  104. {
  105. if( JoomlapackCUBETables::_isSetCUBEInFile())
  106. JoomlapackCUBETables::_deleteCUBEObject();
  107. }
  108. }
  109. /**
  110. * Counts the number of instances for a specific variable
  111. *
  112. * @param string $key The varaible's name
  113. * @return string
  114. */
  115. function CountVar( $key )
  116. {
  117. if($key == 'CUBEObject')
  118. {
  119. if(JoomlapackCUBETables::_isSetCUBEInFile())
  120. {
  121. $cubeTmpFile = JoomlapackCUBETables::ReadVar('cubetmpfile');
  122. return file_exists($cubeTmpFile) ? 1 : 0;
  123. }
  124. }
  125. $db =& JFactory::getDBO();
  126. $sql = 'SELECT `key` FROM '.$db->nameQuote('#__jp_temp').
  127. ' WHERE `key` = '.$db->Quote($key);
  128. $db->setQuery( $sql );
  129. $db->query();
  130. $numRows = $db->getNumRows();
  131. return $numRows;
  132. }
  133. /**
  134. * Reads and unserializes a packvar variable (combo function)
  135. *
  136. * @param string $varName The variable name to read
  137. * @param mixed $default The default unserialized data to return if the $varName doesn't exist
  138. * @return mixed The unserialized value read from database
  139. */
  140. function UnserializeVar( $varName, $default = null )
  141. {
  142. if($varName == 'CUBEObject')
  143. {
  144. if(JoomlapackCUBETables::_isSetCUBEInFile())
  145. {
  146. return JoomlapackCUBETables::_loadCUBEObjectFromFile();
  147. }
  148. }
  149. $count = JoomlapackCUBETables::CountVar($varName);
  150. if( $count >=1 )
  151. {
  152. $serialized = JoomlapackCUBETables::ReadVar($varName);
  153. return unserialize($serialized);
  154. }
  155. else
  156. {
  157. return $default;
  158. }
  159. }
  160. /**
  161. * Writes a serialized copy of the $contentVariable to the database, under the packvar
  162. * variable name of $varName.
  163. *
  164. * @param string $varName The packvar to create
  165. * @param mixed $contentVariable Any variable to serialize (e.g. object, array, other variables, etc)
  166. */
  167. function SerializeVar( $varName, &$contentVariable )
  168. {
  169. if($varName == 'CUBEObject')
  170. {
  171. if(JoomlapackCUBETables::_isSetCUBEInFile())
  172. {
  173. JoomlapackCUBETables::_saveCUBEObjectToFile($contentVariable);
  174. return;
  175. }
  176. }
  177. $serialized = serialize($contentVariable);
  178. JoomlapackCUBETables::WriteVar($varName, $serialized);
  179. }
  180. /**
  181. * Is BASE64 encoding available?
  182. *
  183. * @return bool
  184. */
  185. function _getBase64()
  186. {
  187. static $_hasBase64 = null;
  188. if(is_null($_hasBase64))
  189. {
  190. $_hasBase64 = function_exists('base64_encode') && function_exists('base64_decode');
  191. }
  192. return $_hasBase64;
  193. }
  194. /**
  195. * Checks if the option to store the CUBE file in Joomla's tmp directory is enabled
  196. * @return bool
  197. */
  198. function _isSetCUBEInFile()
  199. {
  200. static $isSetCUBEInFile;
  201. if(is_null($isSetCUBEInFile))
  202. {
  203. $registry =& JoomlapackModelRegistry::getInstance();
  204. $isSetCUBEInFile = $registry->get('cubeinfile', true);
  205. }
  206. return $isSetCUBEInFile;
  207. }
  208. /**
  209. * Returns the stored path to the temporary CUBEObject file, or creates a new one
  210. * @param int $createNew 0 = create if it doesn't exist, 1 = do not create new, 2 = force create new
  211. * @return unknown_type
  212. */
  213. function _cubeTmpFile($createNew = 0)
  214. {
  215. $cubeTmpFile = JoomlapackCUBETables::ReadVar('cubetmpfile');
  216. $createNew = false;
  217. // 1. Search if the filename is not empty
  218. if( ($cubeTmpFile == '') && ($createNew == 0) )
  219. {
  220. JoomlapackLogger::WriteLog(_JP_LOG_INFO, "No CUBEObject storage file set; I will create a new one.");
  221. $createNew = true;
  222. }
  223. else
  224. {
  225. // 2. If the filename is not empty, make sure it exists
  226. $createNew = !file_exists($cubeTmpFile);
  227. if($createNew) JoomlapackLogger::WriteLog(_JP_LOG_INFO, "The CUBEObject storage file doesn't exist on disk; I will create a new one.");
  228. $createNew = $createNew && ($createNew == 0);
  229. $createNew = $createNew || ($createNew == 2);
  230. }
  231. // 3. If the filename was empty or the file doesn't exist, create a fresh copy
  232. if($createNew)
  233. {
  234. $registry =& JoomlapackModelRegistry::getInstance();
  235. $cubeTmpFile = tempnam($registry->getTemporaryDirectory(),'jp');
  236. JoomlapackCUBETables::WriteVar('cubetmpfile',$cubeTmpFile);
  237. JoomlapackLogger::WriteLog(_JP_LOG_INFO, "Creating new CUBEObject storage file: " . $cubeTmpFile );
  238. }
  239. return $cubeTmpFile;
  240. }
  241. /**
  242. * Saves CUBE dump to a temporary file
  243. *
  244. * @param string $contentVariable
  245. */
  246. function _saveCUBEObjectToFile(&$contentVariable)
  247. {
  248. $tmpfile = JoomlapackCUBETables::_cubeTmpFile();
  249. file_put_contents($tmpfile,serialize($contentVariable));
  250. JoomlapackCUBETables::WriteVar('cubetmpfile',$tmpfile);
  251. }
  252. function _loadCUBEObjectFromFile()
  253. {
  254. $tmpfile = JoomlapackCUBETables::_cubeTmpFile();
  255. return unserialize(file_get_contents($tmpfile));
  256. }
  257. function _deleteCUBEObject()
  258. {
  259. $tmpfile = JoomlapackCUBETables::_cubeTmpFile(1);
  260. if(!empty($tmpfile))
  261. {
  262. JoomlapackLogger::WriteLog(_JP_LOG_INFO, "Removing CUBE temporary file: " . $tmpfile );
  263. @unlink($tmpfile);
  264. }
  265. JoomlapackCUBETables::DeleteVar('cubetmpfile');
  266. }
  267. }