PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/QApplication.class.php

http://tracmor.googlecode.com/
PHP | 303 lines | 157 code | 33 blank | 113 comment | 58 complexity | a73d47b35599c6c18cf0a56ccabd8b5d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The Application class is an abstract class that statically provides
  4. * information and global utilities for the entire web application.
  5. *
  6. * Custom constants for this webapp, as well as global variables and global
  7. * methods should be declared in this abstract class (declared statically).
  8. *
  9. * This Application class should extend from the ApplicationBase class in
  10. * the framework.
  11. */
  12. abstract class QApplication extends QApplicationBase {
  13. /**
  14. * This is called by the PHP5 Autoloader. This method overrides the
  15. * one in ApplicationBase.
  16. *
  17. * @return void
  18. */
  19. public static function Autoload($strClassName) {
  20. // First use the Qcodo Autoloader
  21. if (!parent::Autoload($strClassName)) {
  22. // NOTE: Run any custom autoloading functionality (if any) here...
  23. if (file_exists($strFilePath = sprintf('%s/%s.class.php', __DATA_CLASSES__, $strClassName))) {
  24. require($strFilePath);
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. /**
  31. * Method will setup Internationalization.
  32. * NOTE: This method has been INTENTIONALLY left incomplete.
  33. * @return void
  34. */
  35. public static function InitializeI18n() {
  36. if (isset($_SESSION)) {
  37. if (array_key_exists('country_code', $_SESSION))
  38. QApplication::$CountryCode = $_SESSION['country_code'];
  39. if (array_key_exists('language_code', $_SESSION))
  40. QApplication::$LanguageCode = $_SESSION['language_code'];
  41. }
  42. /*
  43. * NOTE: This is where you would implement code to do Language Setting discovery, as well, for example:
  44. * Checking against $_GET['language_code']
  45. * checking against session (example provided below)
  46. * Checking the URL
  47. * etc.
  48. * Options to do this are left to the developer.
  49. */
  50. // Initialize I18n if QApplication::$LanguageCode is set
  51. if (QApplication::$LanguageCode)
  52. QI18n::Initialize();
  53. // Otherwise, you could optionally run with some defaults
  54. else {
  55. // QApplication::$CountryCode = 'us';
  56. // QApplication::$LanguageCode = 'en';
  57. // QI18n::Initialize();
  58. }
  59. }
  60. ////////////////////////////
  61. // QApplication Customizations (e.g. EncodingType, Disallowing PHP Session, etc.)
  62. ////////////////////////////
  63. public static $EncodingType = 'UTF-8';
  64. // public static $EnableSession = false;
  65. // System Wide Settings Object
  66. public static $TracmorSettings;
  67. // User Account Object for logged in user
  68. public static $objUserAccount;
  69. // RoleModule object based on the user that is logged in and the module they are accessing
  70. public static $objRoleModule;
  71. ////////////////////////////
  72. // Additional Static Methods
  73. ////////////////////////////
  74. // NOTE: Define any other custom global WebApplication functions (if any) here...
  75. // Load the Tracmor Settings for global accessibility
  76. public static function LoadTracmorSettings() {
  77. QApplication::$TracmorSettings = new TracmorSettings();
  78. }
  79. // Assign the UserAccountId to a session variable
  80. public static function Login(UserAccount $objUserAccount) {
  81. // Assign the UserAccountId as a session variable
  82. // This is the only variable that is assigned as a session variable, all others are stored in QApplication
  83. $_SESSION['intUserAccountId'] = $objUserAccount->UserAccountId;
  84. }
  85. // Destroy the user session and redirect the user to the login page
  86. public static function Logout() {
  87. QFileFormStateHandler::DeleteFormStateForSession();
  88. unset($_SESSION['intUserAccountId']);
  89. session_destroy();
  90. QApplication::Redirect('../login.php');
  91. }
  92. // Authenticate a certain module based on the module and the Role of the logged in user
  93. public static function Authenticate($intModuleId = null) {
  94. // If logins have been disabled for this site, log the user out
  95. if (QApplication::$TracmorSettings->DisableLogins)
  96. QApplication::Logout();
  97. if (array_key_exists('intUserAccountId', $_SESSION)) {
  98. $objUserAccount = UserAccount::Load($_SESSION['intUserAccountId']);
  99. if ($objUserAccount) {
  100. // Assign the UserAccount object to the globally available QApplication
  101. QApplication::$objUserAccount = $objUserAccount;
  102. // If they are not in the admin panel
  103. if ($intModuleId) {
  104. $objRoleModule = RoleModule::LoadByRoleIdModuleId($objUserAccount->RoleId, $intModuleId);
  105. // If they do not have access to this module
  106. if (!$objRoleModule->AccessFlag) {
  107. QApplication::Redirect('../common/trespass.php');
  108. }
  109. // Assign the RoleModule to QApplication
  110. else {
  111. QApplication::$objRoleModule = $objRoleModule;
  112. }
  113. }
  114. // ModuleId is null for the admin panel
  115. // Check if the user is an admin
  116. elseif (!$objUserAccount->AdminFlag) {
  117. QApplication::Redirect('../common/trespass.php');
  118. }
  119. }
  120. else {
  121. QApplication::Redirect('../common/trespass.php');
  122. }
  123. }
  124. else {
  125. QApplication::Redirect('../login.php?strReferer=' . urlencode(QApplication::$RequestUri));
  126. }
  127. }
  128. /**
  129. * Authorizes any control to determine if the user has access
  130. * If not, it sets the objControl->Visible to false
  131. *
  132. * @param object $objEntity - any entity with a created_by column (asset, location, etc.)
  133. * @param object $objControl - the control which is being evaluated - any QControl where visible is a property
  134. * @param integer $intAuthorizationId - the authorization required to see this control (view(1), edit(2), or delete(3))
  135. */
  136. public static function AuthorizeControl($objEntity, $objControl, $intAuthorizationId, $intModuleId = null) {
  137. if ($intModuleId == null) {
  138. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId(QApplication::$objRoleModule->RoleModuleId, $intAuthorizationId);
  139. }
  140. else {
  141. $objRoleModule = RoleModule::LoadByRoleIdModuleId(QApplication::$objRoleModule->RoleId, $intModuleId);
  142. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId($objRoleModule->RoleModuleId, $intAuthorizationId);
  143. }
  144. // Added if $objEntity == null for the ship button shortcut on the asset page.
  145. if ($objRoleModuleAuthorization->AuthorizationLevelId == 1 || ($objRoleModuleAuthorization->AuthorizationLevelId == 2 && $objEntity == null) || ($objRoleModuleAuthorization->AuthorizationLevelId == 2 && $objEntity->CreatedBy == QApplication::$objUserAccount->UserAccountId)) {
  146. $objControl->Visible = true;
  147. }
  148. else {
  149. $objControl->Visible = false;
  150. }
  151. }
  152. /**
  153. * Authorizes an entity for viewing or editing. If the user is not authorized to view/create this entity, then they are sent to the trespass page.
  154. *
  155. * @param object $objEntity
  156. * @param bool $blnEditMode
  157. */
  158. public static function AuthorizeEntity($objEntity, $blnEditMode) {
  159. // If it is an existing entity, check that the user has 'View' Authorization
  160. if ($blnEditMode) {
  161. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId(QApplication::$objRoleModule->RoleModuleId, 1);
  162. // If the user doesn't have an 'All' Authorization Level, or an 'Owner' Authorization Level and owns this entity, redirect
  163. if ($objRoleModuleAuthorization->AuthorizationLevelId != 1 && !($objRoleModuleAuthorization->AuthorizationLevelId == 2 && $objEntity->CreatedBy == QApplication::$objUserAccount->UserAccountId)) {
  164. QApplication::Redirect('../common/trespass.php');
  165. }
  166. }
  167. // If it is a new entity, check that the user has 'Edit' Authorization
  168. else {
  169. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId(QApplication::$objRoleModule->RoleModuleId, 2);
  170. // The user must have either an 'All' or 'Owner' Authorization Level to create a new entity
  171. if (!$objRoleModuleAuthorization->AuthorizationLevelId == 1 && !$objRoleModuleAuthorization->AuthorizationLevelId == 2) {
  172. QApplication::Redirect('../common/trespass.php');
  173. }
  174. }
  175. }
  176. /**
  177. * Authorizes an entity for editing and returns a boolean value for error checking purposes
  178. *
  179. * @param object $objEntity
  180. * @param integer $intAuthorizationId
  181. * @return bool $blnAuthorized
  182. */
  183. public static function AuthorizeEntityBoolean($objEntity, $intAuthorizationId) {
  184. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId(QApplication::$objRoleModule->RoleModuleId, $intAuthorizationId);
  185. if ($objRoleModuleAuthorization->AuthorizationLevelId != 1 && !($objRoleModuleAuthorization->AuthorizationLevelId == 2 && $objEntity->CreatedBy == QApplication::$objUserAccount->UserAccountId)) {
  186. $blnAuthorized = false;
  187. }
  188. else {
  189. $blnAuthorized = true;
  190. }
  191. return $blnAuthorized;
  192. }
  193. /**
  194. * This function returns the SQL necessary for all Load and Count scripts for list pages
  195. *
  196. * @param string $strEntity 'asset', 'company', e.g., the name of the table
  197. */
  198. public static function AuthorizationSql($strEntity) {
  199. // if $objRoleModule is empty, then they are in the administration module so they have access to everything
  200. if (empty(QApplication::$objRoleModule)) {
  201. $strToReturn = '';
  202. }
  203. else {
  204. // Load the RoleModuleAuthorization
  205. $objRoleModuleAuthorization = RoleModuleAuthorization::LoadByRoleModuleIdAuthorizationId(QApplication::$objRoleModule->RoleModuleId, 1);
  206. if (!$objRoleModuleAuthorization) {
  207. throw new Exception('No valid RoleModuleAuthorization for this User Role.');
  208. }
  209. // Owner - Return only entities where the logged in user is the owner
  210. elseif ($objRoleModuleAuthorization->AuthorizationLevelId == 2) {
  211. $strToReturn = sprintf('AND `%s` . `created_by` = %s', $strEntity, QApplication::$objUserAccount->UserAccountId);
  212. }
  213. // None - Do not return any entities
  214. elseif ($objRoleModuleAuthorization->AuthorizationLevelId == 3) {
  215. $strToReturn = sprintf('AND `%s` . `created_by` = 0', $strEntity);
  216. }
  217. // All - Return all entities, so do not limit the query at all
  218. else {
  219. $strToReturn = '';
  220. }
  221. }
  222. return $strToReturn;
  223. }
  224. /**
  225. * This returns the html for either a check or an x based on the boolean value
  226. *
  227. * @param bool $blnValue
  228. * @return string HTML img tag for the check or the string
  229. */
  230. public static function BooleanImage($blnValue = true) {
  231. if ($blnValue) {
  232. $strToReturn = sprintf('<img src="%s">', '../images/icons/check.png');
  233. }
  234. else {
  235. $strToReturn = sprintf('<img src="%s">', '../images/icons/x.png');
  236. }
  237. return $strToReturn;
  238. }
  239. /**
  240. * This moves a file from the local filesystem to the S3 file system provided in the tracmor_configuration.inc.php file
  241. *
  242. * @param string $strPath should not include trailing slash
  243. * @param string $strFileName
  244. * @param string $strType MIME type of the file
  245. * @param string $strS3Path path to S3 folder (do not include bucket) - '/images/shipping_labels' for example
  246. * @return bool
  247. */
  248. // strPath and strS3Path should not include trailing slash but this will still work if it doesn't
  249. // strS3Path should include beginning slash '/images/shipping_labels' for example
  250. public static function MoveToS3($strPath, $strFileName, $strType, $strS3Path) {
  251. rtrim($strPath, '/');
  252. rtrim($strS3Path, '/');
  253. if (file_exists($strPath . '/' . $strFileName)) {
  254. require_once( __DOCROOT__ . __PHP_ASSETS__ . '/s3.class.php');
  255. $objS3 = new S3();
  256. $objS3->putBucket(AWS_BUCKET);
  257. $fh = fopen($strPath . '/' . $strFileName, 'rb');
  258. $contents = fread($fh, filesize($strPath . '/' . $strFileName));
  259. fclose($fh);
  260. $objS3->putObject($strFileName, $contents, AWS_BUCKET . $strS3Path, 'public-read', $strType);
  261. unlink($strPath . '/' . $strFileName);
  262. unset($objS3);
  263. return true;
  264. }
  265. else {
  266. return false;
  267. }
  268. }
  269. }
  270. ?>