/application/models/CompanyWebsite.class.php

https://github.com/dbernar1/Project-Pier · PHP · 312 lines · 149 code · 30 blank · 133 comment · 28 complexity · a3dcce8c112a1459a449a5f8daea3a11 MD5 · raw file

  1. <?php
  2. /**
  3. * Company website class
  4. *
  5. * @version 1.0
  6. * @http://www.projectpier.org/
  7. */
  8. final class CompanyWebsite {
  9. /** Name of the cookie / session var where we save session_id **/
  10. const USER_SESSION_ID_VAR = 'user_session_id';
  11. /**
  12. * Owner company
  13. *
  14. * @var Company
  15. */
  16. private $company;
  17. /**
  18. * Logged user
  19. *
  20. * @var User
  21. */
  22. private $logged_user;
  23. /**
  24. * Selected project
  25. *
  26. * @var Project
  27. */
  28. private $selected_project;
  29. /**
  30. * Init company website environment
  31. *
  32. * @access public
  33. * @param void
  34. * @return null
  35. * @throws Error
  36. */
  37. function init() {
  38. trace(__FILE__, 'init()');
  39. if (isset($this) && ($this instanceof CompanyWebsite)) {
  40. $this->initCompany();
  41. $this->initActiveProject();
  42. $controller = array_var($_GET, 'c');
  43. //Feed users do not need to be logged in here
  44. if ($controller != 'feed') {
  45. $this->initLoggedUser();
  46. }
  47. } else {
  48. CompanyWebsite::instance()->init();
  49. } // if
  50. } // init
  51. /**
  52. * Init company based on subdomain
  53. *
  54. * @access public
  55. * @param string
  56. * @return null
  57. * @throws Error
  58. */
  59. private function initCompany() {
  60. trace(__FILE__, 'initCompany()');
  61. $company = Companies::getOwnerCompany();
  62. trace(__FILE__, 'initCompany() - company check');
  63. if (!($company instanceof Company)) {
  64. throw new OwnerCompanyDnxError();
  65. } // if
  66. trace(__FILE__, 'initCompany() - admin check');
  67. if (!($company->getCreatedBy() instanceof User)) {
  68. throw new AdministratorDnxError();
  69. } // if
  70. trace(__FILE__, 'initCompany() - setCompany()');
  71. $this->setCompany($company);
  72. } // initCompany
  73. /**
  74. * Init active project, if we have active_project $_GET var
  75. *
  76. * @access public
  77. * @param void
  78. * @return null
  79. * @throws Error
  80. */
  81. private function initActiveProject() {
  82. trace(__FILE__, 'initActiveProject()');
  83. $project_id = array_var($_GET, 'active_project');
  84. if (!empty($project_id)) {
  85. $project = Projects::findById($project_id);
  86. if ($project instanceof Project) {
  87. $this->setProject($project);
  88. } else {
  89. $project = new Project;
  90. $project->setId($project_id);
  91. $project->setName(lang('deleted or unknown'));
  92. //flash_error(lang('failed to load project'));
  93. $this->setProject($project);
  94. //throw new Error(lang('failed to load project'));
  95. } // if
  96. } // if
  97. } // initActiveProject
  98. /**
  99. * This function will use session ID from session or cookie and if presend log user
  100. * with that ID. If not it will simply break.
  101. *
  102. * When this function uses session ID from cookie the whole process will be treated
  103. * as new login and users last login time will be set to current time.
  104. *
  105. * @access public
  106. * @param void
  107. * @return boolean
  108. */
  109. private function initLoggedUser() {
  110. trace(__FILE__, 'initLoggedUser()');
  111. $user_id = Cookie::getValue('id'.TOKEN_COOKIE_NAME);
  112. $twisted_token = Cookie::getValue(TOKEN_COOKIE_NAME);
  113. $remember = (boolean) Cookie::getValue('remember'.TOKEN_COOKIE_NAME, false);
  114. $controller = array_var($_GET, 'c'); // needed to check for RSS feed
  115. if (empty($user_id) || empty($twisted_token)) {
  116. trace(__FILE__, "initLoggedUser():end, user_id=$user_id, twisted_token=$twisted_token session_lifetime=".SESSION_LIFETIME);
  117. return false; // we don't have a user
  118. } // if
  119. $user = Users::findById($user_id);
  120. if (!($user instanceof User)) {
  121. trace(__FILE__, "initLoggedUser():end, user_id=$user_id, not found in database");
  122. return false; // failed to find user
  123. } // if
  124. if (!$user->isValidToken($twisted_token)) {
  125. trace(__FILE__, "initLoggedUser():end, user_id=$user_id, twisted_token=$twisted_token invalid token");
  126. return false; // failed to validate token
  127. } // if
  128. if ($controller == 'feed') {
  129. $this->setLoggedUser($user, $remember, false);
  130. } else {
  131. $session_expires = $user->getLastActivity()->advance(SESSION_LIFETIME, false);
  132. if (DateTimeValueLib::now()->getTimestamp() < $session_expires->getTimestamp()) {
  133. trace(__FILE__, 'initLoggedUser(): session not expired');
  134. $this->setLoggedUser($user, $remember, true);
  135. } else {
  136. trace(__FILE__, 'initLoggedUser(): session expired');
  137. $this->logUserIn($user, $remember);
  138. } // if
  139. } // if
  140. } // initLoggedUser
  141. // ---------------------------------------------------
  142. // Utils
  143. // ---------------------------------------------------
  144. /**
  145. * Log user in
  146. *
  147. * @access public
  148. * @param User $user
  149. * @param boolean $remember
  150. * @return null
  151. */
  152. function logUserIn(User $user, $remember = false) {
  153. trace(__FILE__, 'logUserIn():begin');
  154. $user->setLastLogin(DateTimeValueLib::now());
  155. if (is_null($user->getLastActivity())) {
  156. $user->setLastVisit(DateTimeValueLib::now());
  157. } else {
  158. $user->setLastVisit($user->getLastActivity());
  159. } // if
  160. trace(__FILE__, 'logUserIn():setLoggedUser()');
  161. $this->setLoggedUser($user, $remember, true);
  162. trace(__FILE__, 'logUserIn():end');
  163. } // logUserIn
  164. /**
  165. * Log out user
  166. *
  167. * @access public
  168. * @param void
  169. * @return null
  170. */
  171. function logUserOut() {
  172. $this->logged_user = null;
  173. Cookie::unsetValue('id'.TOKEN_COOKIE_NAME);
  174. Cookie::unsetValue(TOKEN_COOKIE_NAME);
  175. Cookie::unsetValue('remember'.TOKEN_COOKIE_NAME);
  176. setcookie(session_name(),'',time()-3600,'/');
  177. session_unset();
  178. session_destroy();
  179. session_write_close();
  180. session_regenerate_id(true);
  181. } // logUserOut
  182. // ---------------------------------------------------
  183. // Getters and setters
  184. // ---------------------------------------------------
  185. /**
  186. * Get company
  187. *
  188. * @access public
  189. * @param null
  190. * @return Company
  191. */
  192. function getCompany() {
  193. return $this->company;
  194. } // getCompany
  195. /**
  196. * Set company value
  197. *
  198. * @access public
  199. * @param Company $value
  200. * @return null
  201. */
  202. function setCompany(Company $value) {
  203. $this->company = $value;
  204. } // setCompany
  205. /**
  206. * Get logged_user
  207. *
  208. * @access public
  209. * @param null
  210. * @return User
  211. */
  212. function getLoggedUser() {
  213. return $this->logged_user;
  214. } // getLoggedUser
  215. /**
  216. * Set logged_user value
  217. *
  218. * @access public
  219. * @param User $value
  220. * @param boolean $remember Remember this user
  221. * @param boolean $set_last_activity_time Turned off in case of feed login
  222. * @return null
  223. * @throws DBQueryError
  224. */
  225. function setLoggedUser(User $user, $remember = false, $set_last_activity_time = true, $set_cookies = true) {
  226. trace(__FILE__, 'setLoggedUser():begin');
  227. if ($set_last_activity_time) {
  228. $user->setLastActivity(DateTimeValueLib::now());
  229. trace(__FILE__, 'setLoggedUser():user->save()');
  230. $user->save();
  231. } // if
  232. if ($set_cookies) {
  233. $expiration = $remember ? config_option('remember_login_lifetime', 3600) : 3600;
  234. Cookie::setValue('id'.TOKEN_COOKIE_NAME, $user->getId(), $expiration);
  235. Cookie::setValue(TOKEN_COOKIE_NAME, $user->getTwistedToken(), $expiration);
  236. if ($remember) {
  237. Cookie::setValue('remember'.TOKEN_COOKIE_NAME, 1, $expiration);
  238. } else {
  239. Cookie::unsetValue('remember'.TOKEN_COOKIE_NAME);
  240. } // if
  241. } // if
  242. trace(__FILE__, 'setLoggedUser():end');
  243. $this->logged_user = $user;
  244. } // setLoggedUser
  245. /**
  246. * Get project
  247. *
  248. * @access public
  249. * @param null
  250. * @return Project
  251. */
  252. function getProject() {
  253. return $this->selected_project;
  254. } // getProject
  255. /**
  256. * Set project value
  257. *
  258. * @access public
  259. * @param Project $value
  260. * @return null
  261. */
  262. function setProject($value) {
  263. if (is_null($value) || ($value instanceof Project)) {
  264. $this->selected_project = $value;
  265. }
  266. } // setProject
  267. /**
  268. * Return single CompanyWebsite instance
  269. *
  270. * @access public
  271. * @param void
  272. * @return CompanyWebsite
  273. */
  274. static function instance() {
  275. static $instance;
  276. if (!($instance instanceof CompanyWebsite)) {
  277. $instance = new CompanyWebsite();
  278. } // if
  279. return $instance;
  280. } // instance
  281. } // CompanyWebsite
  282. ?>