PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/cubi/openbiz/bin/SessionContext.php

http://openbiz-cubi.googlecode.com/
PHP | 409 lines | 228 code | 34 blank | 147 comment | 47 complexity | f1981308858069cc5ab1f99f9353fe9d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?PHP
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @package openbiz.bin
  11. * @copyright Copyright (c) 2005-2011, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id: SessionContext.php 4105 2011-05-06 08:48:08Z jixian2003 $
  15. */
  16. /**
  17. * Index of $_SESSION array that saves all information regarding statefull objects
  18. */
  19. define('OB_STATEFUL_DATA_SESSION_INDEX', 'ob_stateful_data');
  20. define('OB_TRANSIENT_DATA_SESSION_INDEX', 'ob_transient_data');
  21. /**
  22. * SessionContext class is Session management class that has additional methods
  23. * to save/get session variables of metadata based stateful objects
  24. * through their GetSessionVars|SetSessionVars interfaces
  25. *
  26. * @package openbiz.bin
  27. * @author Rocky Swen <rocky@phpopenbiz.org>
  28. * @copyright Copyright (c) 2005-2009, Rocky Swen
  29. * @access public
  30. */
  31. class SessionContext
  32. {
  33. protected $_lastAccessTime;
  34. protected $_timeOut = false;
  35. protected $_sessObjArr = null;
  36. protected $_statefulSessObjArr = null;
  37. protected $_viewHistory = null;
  38. protected $_prevViewObjNames = array();
  39. /**
  40. * Constructor of SessionContext, init session and set session file path
  41. *
  42. * @return void
  43. **/
  44. function __construct()
  45. {
  46. // get session save handler MC or DB
  47. if (defined("SESSION_HANDLER") && SESSION_HANDLER != "" && defined('USE_CUSTOM_SESSION_HANDLER') && USE_CUSTOM_SESSION_HANDLER ==true) {
  48. include_once SESSION_HANDLER.".php";
  49. }
  50. else {
  51. if(!file_exists(SESSION_PATH)){
  52. @mkdir(SESSION_PATH,0777,true);
  53. }
  54. // default is file session
  55. if (defined('SESSION_PATH') && is_writable(SESSION_PATH))
  56. session_save_path(SESSION_PATH);
  57. // we cannot write in the session save path; aborting
  58. if(!is_writable(session_save_path()))
  59. trigger_error("Unable to write in the session save path [".session_save_path()."]", E_USER_ERROR);
  60. }
  61. ini_set('session.gc_probability', 1); // force gc
  62. ini_set('session.gc_divisor', 100);
  63. if (defined('TIMEOUT') && TIMEOUT > 0)
  64. ini_set("session.gc_maxlifetime", TIMEOUT);
  65. else
  66. ini_set("session.gc_maxlifetime", 21600); // 6 hours
  67. if (!defined('CLI') || CLI == 0) {
  68. if(isset($_REQUEST['cubi_sess_id'])){
  69. session_id($_REQUEST['cubi_sess_id']);
  70. }
  71. session_start();
  72. }
  73. // record access time
  74. $curTime = time();
  75. if (isset($_SESSION["LastAccessTime"]))
  76. $this->_lastAccessTime = $_SESSION["LastAccessTime"];
  77. else
  78. $this->_lastAccessTime = $curTime;
  79. $_SESSION["LastAccessTime"] =$curTime;
  80. // see if timeout
  81. $this->_timeOut = false;
  82. if ((TIMEOUT > 0) && (($curTime - $this->_lastAccessTime) > TIMEOUT))
  83. $this->_timeOut = true;
  84. }
  85. /**
  86. * Set single session variable
  87. *
  88. * @param string $varName
  89. * @param mixed $value
  90. * @return void
  91. **/
  92. public function setVar($varName, $value)
  93. {
  94. $_SESSION[$varName] = $value;
  95. }
  96. public function mergeVar($varName, $value)
  97. {
  98. $var = $_SESSION[$varName];
  99. if(is_array($var)){
  100. foreach($value as $key=>$value){
  101. $var[$key]=$value;
  102. }
  103. $_SESSION[$varName] = $var;
  104. }else{
  105. $_SESSION[$varName] = $value;
  106. }
  107. }
  108. /**
  109. * Get single session variable
  110. *
  111. * @param string $varName name of session variable
  112. * @return string
  113. **/
  114. public function getVar($varName)
  115. {
  116. if (!isset($_SESSION[$varName]))
  117. return "";
  118. return $_SESSION[$varName];
  119. }
  120. /**
  121. * Clear/Unset single session variable
  122. * NOTE: NYU - not yet used
  123. *
  124. * @param string $varName
  125. * @return void
  126. **/
  127. public function clearVar($varName)
  128. {
  129. unset($_SESSION[$varName]);
  130. }
  131. /**
  132. * Is variable exist in the session
  133. *
  134. * @param string $varName variable name that checked
  135. * @return boolean TRUE if the var exists in the session, otherwise FALSE
  136. **/
  137. public function varExists($varName)
  138. {
  139. $exists = (array_key_exists($varName, $_SESSION)) ? TRUE : FALSE;
  140. return $exists;
  141. }
  142. public function getNamespace()
  143. {
  144. $view = BizSystem::instance()->getCurrentViewName();
  145. if ($view)
  146. $namespace = $view;
  147. else
  148. $namespace = 'DEFAULT_NS';
  149. return $namespace;
  150. }
  151. /**
  152. * Set single session variable of a stateful object
  153. *
  154. * @param string $objName - object name
  155. * @param string $varName - vaiable name
  156. * @param mixed $value - reference of the value (in/out)
  157. * @param boolean $stateful - is stateful?
  158. * @return void
  159. **/
  160. public function setObjVar($objName, $varName, &$value, $stateful=false )
  161. {
  162. if(preg_match('/\./si',$objName)){
  163. $objName = $this->getNamespace().'#'.$objName;
  164. }
  165. if (!$stateful)
  166. $this->_sessObjArr[$objName][$varName] = $value;
  167. else
  168. $this->_statefulSessObjArr[$objName][$varName] = $value;
  169. }
  170. /**
  171. * Clean Object
  172. *
  173. * @param string $objName object name
  174. * @param boolean $stateful
  175. * @return void
  176. */
  177. public function cleanObj($objName, $stateful=false)
  178. {
  179. if(preg_match('/\./si',$objName)){
  180. $objName = $this->getNamespace().'#'.$objName;
  181. }
  182. if (!$stateful)
  183. unset($this->_sessObjArr[$objName]);
  184. else
  185. unset($this->_statefulSessObjArr[$objName]);
  186. }
  187. /**
  188. * Get single session variable of a stateful object
  189. *
  190. * @param string $objName - object name
  191. * @param string $varName - vaiable name
  192. * @param mixed $value - reference of the value (in/out)
  193. * @param boolean $stateful - is stateful?
  194. * @return void
  195. */
  196. public function getObjVar($objName, $varName, &$value, $stateful=false)
  197. {
  198. if(preg_match('/\./si',$objName)){
  199. $objName = $this->getNamespace().'#'.$objName;
  200. }
  201. if (!$stateful)
  202. {
  203. if (!$this->_sessObjArr)
  204. return;
  205. if (isset($this->_sessObjArr[$objName][$varName]))
  206. $value = $this->_sessObjArr[$objName][$varName];
  207. }
  208. else
  209. {
  210. if (!$this->_statefulSessObjArr)
  211. return;
  212. if (isset($this->_statefulSessObjArr[$objName][$varName]))
  213. $value = $this->_statefulSessObjArr[$objName][$varName];
  214. }
  215. }
  216. /**
  217. * Save session variables of all stateful objects into sessionid_obj file
  218. *
  219. * @return void
  220. **/
  221. public function saveSessionObjects()
  222. {
  223. // loop all objects (bizview, bizform, bizdataobj) collect their session vars
  224. $allobjs = BizSystem::objectFactory()->getAllObjects();
  225. foreach ($allobjs as $obj)
  226. {
  227. if (method_exists($obj, "SetSessionVars"))
  228. {
  229. //after calling $obj->setSessionVars SessObjArr and StatefulSessObjArr are filled
  230. $obj->setSessionVars($this);
  231. }
  232. // if previous view's object is used in current view, don't discard its session data
  233. if (isset($obj->m_Name) && key_exists($obj->m_Name, $this->_prevViewObjNames)) {
  234. unset($this->_prevViewObjNames[$obj->m_Name]);
  235. BizSystem::log(LOG_ERR, "SESSION", "unset ".$obj->m_Name);
  236. }
  237. }
  238. // discard useless previous view's session objects
  239. //foreach($this->_prevViewObjNames as $objName=>$tmp)
  240. // unset($this->_sessObjArr[$objName]);
  241. $this->_sessObjArr["ViewHist"] = $this->_viewHistory;
  242. $this->setVar(OB_TRANSIENT_DATA_SESSION_INDEX, $this->_sessObjArr);
  243. $this->setVar(OB_STATEFUL_DATA_SESSION_INDEX, $this->_statefulSessObjArr);
  244. }
  245. /**
  246. * Retrieve/Get session variables of all stateful objects from sessionid_obj file
  247. *
  248. * @return void
  249. **/
  250. public function retrieveSessionObjects()
  251. {
  252. $this->_sessObjArr = $this->getVar(OB_TRANSIENT_DATA_SESSION_INDEX);
  253. $this->_statefulSessObjArr = $this->getVar(OB_STATEFUL_DATA_SESSION_INDEX);
  254. if (!is_array($this->_sessObjArr))
  255. $this->_sessObjArr = array();
  256. if (!is_array($this->_statefulSessObjArr))
  257. $this->_statefulSessObjArr = array();
  258. $this->_viewHistory = array_key_exists('ViewHist', $this->_sessObjArr)
  259. ? $this->_sessObjArr["ViewHist"]
  260. : NULL;
  261. return TRUE;
  262. }
  263. /**
  264. * Clear session variables of all stateful objects
  265. *
  266. * @param boolean $keepObjects
  267. * @return void
  268. */
  269. public function clearSessionObjects($keepObjects = false)
  270. {
  271. if ($keepObjects == false)
  272. {
  273. unset($this->_sessObjArr);
  274. $this->_sessObjArr = array();
  275. }
  276. else // add previous view's session object names in to a map
  277. {
  278. if (isset($this->_sessObjArr))
  279. {
  280. foreach($this->_sessObjArr as $objName=>$sessobj)
  281. {
  282. //echo "save sess $objName <br/>";
  283. $this->_prevViewObjNames[$objName] = 1;
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * Save a JSON array in session
  290. *
  291. * @param string $jsonValue
  292. * @param string $jsonName
  293. * @return void
  294. **/
  295. public function saveJSONArray($jsonValue, $jsonName = NULL)
  296. {
  297. $jsonArray = json_decode($jsonValue);
  298. if((bool)$jsonName)
  299. { //If I want save all array in session I send the name of the array in session
  300. $this->setVar($jsonName, $jsonArray);
  301. }
  302. else
  303. {//I save each value in session
  304. foreach($jsonArray as $varName=>$value)
  305. {
  306. $this->setVar($varName, $value);
  307. }
  308. }
  309. }
  310. /**
  311. * Get view history data of given BizForm/EasyForm from saved in session file
  312. *
  313. * @param string $formName - name of BizForm/EasyForm
  314. * @return array - view history data represented by an associated array
  315. **/
  316. public function getViewHistory($formName)
  317. {
  318. $view = BizSystem::instance()->getCurrentViewName();
  319. $view_form = $formName; //$view."_".$formname;
  320. return $this->_viewHistory[$view_form];
  321. }
  322. /**
  323. * Set view history data of given bizform into session file
  324. *
  325. * @param string $formName - name of bizform
  326. * @param array $historyInfo - view history data represented by an associated array
  327. * @return void
  328. **/
  329. public function setViewHistory($formName, $historyInfo)
  330. {
  331. $view = BizSystem::instance()->getCurrentViewName();
  332. $view_form = $formName; //$view."_".$formname;
  333. if (!$historyInfo)
  334. unset($this->_viewHistory[$view_form]);
  335. else
  336. $this->_viewHistory[$view_form] = $historyInfo;
  337. }
  338. /**
  339. * Destroy/free all session data of the current session
  340. *
  341. * @return void
  342. **/
  343. public function destroy()
  344. {
  345. unset($this->_viewHistory);
  346. unset($this->_sessObjArr);
  347. unset($this->_statefulSessObjArr);
  348. session_destroy();
  349. }
  350. /**
  351. * Check if user logged in or not
  352. * NOTE: NYU - not yet used
  353. *
  354. * @return boolean
  355. */
  356. public function isUserValid()
  357. {
  358. if (CHECKUSER == "N")
  359. return true;
  360. if ($this->getVar("UserId") != "")
  361. return true;
  362. else
  363. return false;
  364. }
  365. /**
  366. * Check if current session is timeout
  367. *
  368. * @return boolean
  369. **/
  370. public function isTimeout()
  371. {
  372. return $this->_timeOut;
  373. }
  374. }
  375. ?>