PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/include/init.php

http://simpleinvoices.googlecode.com/
PHP | 319 lines | 184 code | 53 blank | 82 comment | 31 complexity | 39394d58ccb4626a2c3896fb36834816 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0, LGPL-3.0
  1. <?php
  2. /*
  3. * Zend framework init - start
  4. */
  5. // ToDo: Remove this include
  6. set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(APPLICATION_PATH ) . "/sys/include/class"));
  7. // Need the configuration beforehand
  8. include_once(APPLICATION_PATH . '/config/define.php');
  9. /*
  10. * Make sure the logging folder and file exist
  11. */
  12. $logFile = APPLICATION_PATH . "/tmp/log/si.log";
  13. // Create tmp log if it does not exist
  14. if (!file_exists(dirname($logFile))) {
  15. if (!mkdir(dirname($logFile), 0770, true)) {
  16. simpleInvoicesError('notWriteable','folder', dirname($logFile));
  17. }
  18. } elseif(!is_writeable(dirname($logFile))) {
  19. if(!chmod(dirname($logFile), 0770)) {
  20. simpleInvoicesError('notWriteable','folder', dirname($logFile));
  21. }
  22. }
  23. if (!is_file($logFile))
  24. {
  25. $createLogFile = fopen($logFile, 'w') or die(simpleInvoicesError('notWriteable','folder', APPLICATION_PATH . '/tmp/log'));
  26. fclose($createLogFile);
  27. }
  28. if (!is_writable($logFile)) {
  29. simpleInvoicesError('notWriteable','file',$logFile);
  30. }
  31. /*
  32. * Make sure the cache folder exists
  33. */
  34. if (!file_exists(APPLICATION_PATH . '/tmp/cache')) {
  35. if (!mkdir(APPLICATION_PATH . '/tmp/cache', 0770, true)) {
  36. simpleInvoicesError('notWriteable','folder', APPLICATION_PATH . '/tmp/cache');
  37. }
  38. } elseif(!is_writeable(APPLICATION_PATH . '/tmp/cache')) {
  39. if(!chmod(APPLICATION_PATH . '/tmp/cache', 0770)) {
  40. simpleInvoicesError('notWriteable','folder', APPLICATION_PATH . '/tmp/cache');
  41. }
  42. }
  43. /*
  44. * Make sure the sessions folder exists
  45. */
  46. if (isset($config->resources->session->save_path)) {
  47. if (!file_exists($config->resources->session->save_path)) {
  48. if (!mkdir($config->resources->session->save_path, 0770, true)) {
  49. simpleInvoicesError('notWriteable','folder', $config->resources->session->save_path);
  50. }
  51. } elseif(!is_writeable($config->resources->session->save_path)) {
  52. if(!chmod($config->resources->session->save_path, 0770)) {
  53. simpleInvoicesError('notWriteable','folder', $config->resources->session->save_path);
  54. }
  55. }
  56. }
  57. /**
  58. * Backward compatibility
  59. *
  60. * Gateway between Bootstrap and Init
  61. * Some global variables may be removed, however, to get a fast start
  62. * we just get them from the Zend_Registry and go along as up until now
  63. */
  64. $config = Zend_Registry::get('config');
  65. $auth_session = Zend_Registry::get('auth_session');
  66. $logger = Zend_Registry::get('logger');
  67. // ToDo: Delete this once it can be fetched in another way
  68. $baseUrl = $config->resources->frontController->baseUrl;
  69. if ($baseUrl == '/') $baseUrl = '';
  70. Zend_Registry::set('baseUrl', $baseUrl);
  71. //start use of zend_cache
  72. $frontendOptions = array(
  73. 'lifetime' => 7200, // cache lifetime of 2 hours
  74. 'automatic_serialization' => true
  75. );
  76. /*
  77. * Zend framework init - end
  78. */
  79. /*
  80. * Smarty inint - start
  81. */
  82. #ini_set('display_errors',true);
  83. require_once("smarty/Smarty.class.php");
  84. require_once("lib/paypal/paypal.class.php");
  85. require_once('lib/HTMLPurifier/HTMLPurifier.standalone.php');
  86. require_once('sys/include/functions.php');
  87. //ob_start('addCSRFProtection');
  88. /*
  89. * Zend Framework cache section - start
  90. * -- must come after the tmp dir writeable check
  91. */
  92. // getting a Zend_Cache_Core object
  93. try {
  94. $backendOptions = array(
  95. 'cache_dir' => APPLICATION_PATH . '/tmp/cache/' // Directory where to put the cache files
  96. );
  97. $cache = Zend_Cache::factory('Core',
  98. 'File',
  99. $frontendOptions,
  100. $backendOptions);
  101. //required for some servers
  102. // This makes it active for Zend_Locale as well
  103. Zend_Date::setOptions(array('cache' => $cache));
  104. }
  105. catch (Exception $e) {
  106. echo 'Caught exception: ', $e->getMessage(), "\n";
  107. }
  108. /*
  109. * Zend Framework cache section - end
  110. */
  111. $smarty = new Smarty();
  112. $smarty->debugging = false;
  113. $smarty->cache_lifetime = 0;
  114. // Set paths for Smarty
  115. $smarty->setTemplateDir(realpath(dirname(APPLICATION_PATH) . '/sys/templates/'));
  116. $smarty->setCacheDir(realpath(APPLICATION_PATH . '/tmp/cache/'));
  117. $smarty->setCompileDir(realpath(APPLICATION_PATH . '/tmp/cache/'));
  118. //adds own smarty plugins
  119. $smarty->addPluginsDir(dirname(__FILE__) . '/smarty_plugins/');
  120. //add stripslash smarty function
  121. $smarty->registerPlugin('modifier', 'unescape', 'stripslashes');
  122. $smarty->registerPlugin('modifier', 'capitalise', 'ucwords');
  123. $smarty->registerPlugin('modifier', "siLocal_number", array("siLocal", "number"));
  124. $smarty->registerPlugin('modifier', "siLocal_number_clean", array("siLocal", "number_clean"));
  125. $smarty->registerPlugin('modifier', "siLocal_number_trim", array("siLocal", "number_trim"));
  126. $smarty->registerPlugin('modifier', "siLocal_number_formatted", array("siLocal", "number_formatted"));
  127. $smarty->registerPlugin('modifier', "siLocal_date", array("siLocal", "date"));
  128. $smarty->registerPlugin('modifier', 'htmlsafe', 'htmlsafe');
  129. $smarty->registerPlugin('modifier', 'urlsafe', 'urlsafe');
  130. $smarty->registerPlugin('modifier', 'urlencode', 'urlencode');
  131. $smarty->registerPlugin('modifier', 'outhtml', 'outhtml');
  132. $smarty->registerPlugin('modifier', 'htmlout', 'outhtml'); //common typo
  133. $smarty->registerPlugin('modifier', 'urlescape', 'urlencode'); //common typo
  134. $path = pathinfo($_SERVER['REQUEST_URI']);
  135. //SC: Install path handling will need changes if used in non-HTML contexts
  136. $install_path = htmlsafe($path['dirname']);
  137. //set up app with relevant php setting
  138. date_default_timezone_set($config->phpSettings->date->timezone);
  139. error_reporting($config->debug->error_reporting);
  140. ini_set('display_startup_errors', $config->phpSettings->display_startup_errors);
  141. ini_set('display_errors', $config->phpSettings->display_errors);
  142. ini_set('log_errors', $config->phpSettings->log_errors);
  143. ini_set('error_log', $config->phpSettings->error_log);
  144. //after config is loaded - do auth
  145. require_once($include_dir . 'sys/include/include_auth.php');
  146. include_once("sys/include/class/db.php");
  147. include_once("sys/include/class/index.php");
  148. $db = db::getInstance();
  149. include_once("sys/include/sql_queries.php");
  150. $install_tables_exists = SimpleInvoices_Db::tableExists("biller");
  151. if ($install_tables_exists == true)
  152. {
  153. $install_data_exists = SimpleInvoices_Db::dataExists();
  154. }
  155. //TODO - add this as a function in sql_queries.php or a class file
  156. //if ( ($install_tables_exists != false) AND ($install_data_exists != false) )
  157. if ( $install_tables_exists != false )
  158. {
  159. if (SimpleInvoices_Db_Table_SQLPatchManager::getNumberOfDoneSQLPatches() > "196")
  160. {
  161. $SI_EXTENSIONS = new SimpleInvoices_Db_Table_Extensions();
  162. // ToDo: Watch out! This get's translated while the original doesn't!!!
  163. $extensions = $SI_EXTENSIONS->fetchAll();
  164. $DB_extensions = array();
  165. foreach($extensions as $this_extension) {
  166. $DB_extensions[$this_extension['name']] = $this_extension;
  167. }
  168. /*
  169. $sql="SELECT * from ".TB_PREFIX."extensions WHERE (domain_id = :id OR domain_id = 0 ) ORDER BY domain_id ASC";
  170. $sth = dbQuery($sql,':id', $auth_session->domain_id ) or die(htmlsafe(end($dbh->errorInfo())));
  171. while ( $this_extension = $sth->fetch() )
  172. {
  173. $DB_extensions[$this_extension['name']] = $this_extension;
  174. }
  175. */
  176. $config->extension = $DB_extensions;
  177. }
  178. }
  179. /*
  180. * Moved the extensions init loop out of index so can load extensions stuff before acl code
  181. */
  182. if ($config->extension != null)
  183. {
  184. foreach($config->extension as $extension)
  185. {
  186. /*
  187. * If extension is enabled then continue and include the requested file for that extension if it exists
  188. */
  189. if($extension->enabled == "1")
  190. {
  191. if(file_exists($include_dir . "sys/extensions/$extension->name/include/init.php"))
  192. {
  193. require_once("sys/extensions/$extension->name/include/init.php");
  194. }
  195. }
  196. }
  197. }
  198. // If no extension loaded, load Core
  199. if (! $config->extension)
  200. {
  201. $extension_core = new Zend_Config(array('core'=>array(
  202. 'id'=>1,
  203. 'domain_id'=>1,
  204. 'name'=>'core',
  205. 'description'=>'Core part of Simple Invoices - always enabled',
  206. 'enabled'=>1
  207. )));
  208. $config->extension = $extension_core;
  209. }
  210. require_once($include_dir . 'sys/include/language.php');
  211. //add class files for extensions
  212. //checkConnection();
  213. require_once($include_dir . 'sys/include/manageCustomFields.php');
  214. require_once($include_dir . "sys/include/validation.php");
  215. //if authentication enabled then do acl check etc..
  216. if ($config->authentication->enabled == 1 )
  217. {
  218. require_once($include_dir . "sys/include/acl.php");
  219. require_once($include_dir . "sys/include/check_permission.php");
  220. }
  221. /*
  222. Array: Early_exit
  223. - Pages that don't need header or exit prior to adding the template add in here
  224. */
  225. $early_exit = array();
  226. $early_exit[] = "auth_login";
  227. $early_exit[] = "api_cron";
  228. $early_exit[] = "auth_logout";
  229. $early_exit[] = "export_pdf";
  230. $early_exit[] = "export_invoice";
  231. $early_exit[] = "statement_export";
  232. $early_exit[] = "invoice_template";
  233. $early_exit[] = "payments_print";
  234. #$early_exit[] = "reports_report_statement";
  235. $early_exit[] = "documentation_view";
  236. $early_exit[] = "biller_upload-logo";
  237. //$early_exit[] = "install_index";
  238. $early_exit[] = "export_payment";
  239. $module = isset($_GET['module'])?$_GET['module']:null;
  240. switch ($module)
  241. {
  242. case "export" :
  243. $smarty_output = "fetch";
  244. break;
  245. default :
  246. $smarty_output = "display";
  247. break;
  248. }
  249. //get the url - used for templates / pdf
  250. $siUrl = getURL();
  251. //zend db
  252. // Get extensions from DB, and update config array
  253. //If using the folowing line, the DB settings should be appended to the config array, instead of replacing it (NOT TESTED!)
  254. //$config->extension() = $DB_extensions;
  255. if (isset($install_data_exists) && $install_data_exists) {
  256. $SI_SYSTEM_DEFAULTS = new SimpleInvoices_Db_Table_SystemDefaults();
  257. $defaults = $SI_SYSTEM_DEFAULTS->fetchAll();
  258. } else {
  259. $defaults = array();
  260. }
  261. $smarty->assignGlobal("defaults",$defaults);
  262. $smarty->assignGlobal('baseUrl', $config->resources->frontController->baseUrl);
  263. // Get rid of some errors
  264. $smarty->assign('subPageActive','');
  265. ?>