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

/web/concrete/core/libraries/loader.php

https://bitbucket.org/hudsonite/concrete5
PHP | 466 lines | 334 code | 50 blank | 82 comment | 86 complexity | 37b79a05fe2571ed6b0367742597c713 MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. /**
  3. * @package Core
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. *
  9. */
  10. /**
  11. * A wrapper for loading core files, libraries, applications and models. Whenever possible the loader class should be used because it will always know where to look for the proper files, in the proper order.
  12. * @package Core
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @category Concrete
  15. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  16. * @license http://www.concrete5.org/license/ MIT License
  17. */
  18. class Concrete5_Library_Loader {
  19. static $autoloadClasses = array();
  20. /**
  21. * Loads a library file, either from the site's files or from Concrete's
  22. */
  23. public function library($lib, $pkgHandle = null) {
  24. $env = Environment::get();
  25. require_once($env->getPath(DIRNAME_LIBRARIES . '/' . $lib . '.php', $pkgHandle));
  26. }
  27. /**
  28. * Loads a job file, either from the site's files or from Concrete's
  29. */
  30. public function job($job, $pkgHandle = null) {
  31. $env = Environment::get();
  32. require_once($env->getPath(DIRNAME_JOBS . '/' . $job . '.php', $pkgHandle));
  33. }
  34. /**
  35. * Loads a model from either an application, the site, or the core Concrete directory
  36. */
  37. public function model($mod, $pkgHandle = null) {
  38. $env = Environment::get();
  39. $r = self::legacyModel($mod);
  40. if (!$r) {
  41. require_once($env->getPath(DIRNAME_MODELS . '/' . $mod . '.php', $pkgHandle));
  42. }
  43. }
  44. protected function legacyModel($model) {
  45. switch($model) {
  46. case 'collection_attributes':
  47. self::model('attribute/categories/collection');
  48. return true;
  49. break;
  50. case 'user_attributes':
  51. self::model('attribute/categories/user');
  52. return true;
  53. break;
  54. case 'file_attributes':
  55. self::model('attribute/categories/file');
  56. return true;
  57. break;
  58. default:
  59. return false;
  60. break;
  61. }
  62. }
  63. /**
  64. * @access private
  65. */
  66. public function packageElement($file, $pkgHandle, $args = null) {
  67. self::element($file, $args, $pkgHandle);
  68. }
  69. /**
  70. * Loads an element from C5 or the site
  71. */
  72. public function element($file, $args = null, $pkgHandle= null) {
  73. if (is_array($args)) {
  74. extract($args);
  75. }
  76. $env = Environment::get();
  77. include($env->getPath(DIRNAME_ELEMENTS . '/' . $file . '.php', $pkgHandle));
  78. }
  79. /**
  80. * Loads a tool file from c5 or site
  81. */
  82. public function tool($file, $args = null, $pkgHandle= null) {
  83. if (is_array($args)) {
  84. extract($args);
  85. }
  86. $env = Environment::get();
  87. require_once($env->getPath(DIRNAME_TOOLS . '/' . $file . '.php', $pkgHandle));
  88. }
  89. /**
  90. * Registers a component with concrete5's autoloader.
  91. */
  92. public static function registerAutoload($classes) {
  93. foreach($classes as $class => $data) {
  94. if (strpos($class, ',') > -1) {
  95. $subclasses = explode(',', $class);
  96. foreach($subclasses as $subclass) {
  97. self::$autoloadClasses[$subclass] = $data;
  98. }
  99. } else {
  100. self::$autoloadClasses[$class] = $data;
  101. }
  102. }
  103. }
  104. protected static function getFileFromCorePath($found) {
  105. $classes = self::$autoloadClasses;
  106. $cl = $classes[$found];
  107. if ($cl) {
  108. $file = $cl[1];
  109. } else {
  110. $file = str_replace('_', '/', $found);
  111. $path = explode('/', $file);
  112. if (count($path) > 0) {
  113. $file = '';
  114. for ($i = 0; $i < count($path); $i++) {
  115. $p = $path[$i];
  116. $file .= Object::uncamelcase($p);
  117. if (($i + 1) < count($path)) {
  118. $file .= '/';
  119. }
  120. }
  121. } else {
  122. $file = Object::uncamelcase($file);
  123. }
  124. }
  125. return $file;
  126. }
  127. public static function autoloadCore($class) {
  128. if (stripos($class, $m = 'Concrete5_Model_') === 0) {
  129. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  130. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_MODELS . '/' . $file . '.php');
  131. }
  132. elseif (stripos($class, $m = 'Concrete5_Library_') === 0) {
  133. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  134. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_LIBRARIES . '/' . $file . '.php');
  135. }
  136. elseif (stripos($class, $m = 'Concrete5_Helper_') === 0) {
  137. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  138. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_HELPERS . '/' . $file . '.php');
  139. }
  140. elseif (stripos($class, $m = 'Concrete5_Controller_Block_') === 0) {
  141. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  142. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_BLOCKS . '/' . $file. '.php');
  143. }
  144. elseif (stripos($class, $m = 'Concrete5_Controller_PageType_') === 0) {
  145. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  146. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGE_TYPES . '/' . $file. '.php');
  147. }
  148. elseif (stripos($class, $m = 'Concrete5_Controller_AttributeType_') === 0) {
  149. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  150. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $file . '.php');
  151. }
  152. elseif (stripos($class, $m = 'Concrete5_Controller_') === 0) {
  153. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  154. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGES . '/' . $file . '.php');
  155. }
  156. elseif (stripos($class, $m = 'Concrete5_Job_') === 0) {
  157. $file = self::getFileFromCorePath(substr($class, strlen($m)));
  158. require_once(DIR_BASE_CORE . '/' . DIRNAME_CORE_CLASSES . '/' . DIRNAME_JOBS . '/' . $file . '.php');
  159. }
  160. }
  161. /**
  162. * @private
  163. */
  164. public static function autoload($class) {
  165. $classes = self::$autoloadClasses;
  166. $cl = $classes[$class];
  167. if ($cl) {
  168. call_user_func_array(array(__CLASS__, $cl[0]), array($cl[1], $cl[2]));
  169. } else {
  170. /* lets handle some things slightly more dynamically */
  171. if (strpos($class, 'BlockController') > 0) {
  172. $class = substr($class, 0, strpos($class, 'BlockController'));
  173. $handle = Object::uncamelcase($class);
  174. self::block($handle);
  175. } else if (strpos($class, 'AttributeType') > 0) {
  176. $class = substr($class, 0, strpos($class, 'AttributeType'));
  177. $handle = Object::uncamelcase($class);
  178. $at = AttributeType::getByHandle($handle);
  179. } else if (strpos($class, 'Helper') > 0) {
  180. $class = substr($class, 0, strpos($class, 'Helper'));
  181. $handle = Object::uncamelcase($class);
  182. $handle = preg_replace('/^site_/', '', $handle);
  183. self::helper($handle);
  184. }
  185. }
  186. }
  187. /**
  188. * Loads a block's controller/class into memory.
  189. * <code>
  190. * <?php self::block('autonav'); ?>
  191. * </code>
  192. */
  193. public function block($bl) {
  194. $db = self::db();
  195. $pkgHandle = $db->GetOne('select pkgHandle from Packages left join BlockTypes on BlockTypes.pkgID = Packages.pkgID where BlockTypes.btHandle = ?', array($bl));
  196. $env = Environment::get();
  197. require_once($env->getPath(DIRNAME_BLOCKS . '/' . $bl . '/' . FILENAME_BLOCK_CONTROLLER, $pkgHandle));
  198. }
  199. /**
  200. * Loads the various files for the database abstraction layer. We would bundle these in with the db() method below but
  201. * these need to be loaded before the models which need to be loaded before db()
  202. */
  203. public function database() {
  204. require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb.inc.php');
  205. require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-exceptions.inc.php');
  206. require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-active-record.inc.php');
  207. require(DIR_BASE_CORE . '/libraries/3rdparty/adodb/adodb-xmlschema03.inc.php');
  208. require(DIR_BASE_CORE . '/libraries/database.php');
  209. }
  210. /**
  211. * Returns the database object, or loads it if not yet created
  212. * <code>
  213. * <?php
  214. * $db = Loader::db();
  215. * $db->query($sql);
  216. * </code>
  217. */
  218. public function db($server = null, $username = null, $password = null, $database = null, $create = false, $autoconnect = true) {
  219. static $_dba;
  220. if ((!isset($_dba) || $create) && ($autoconnect)) {
  221. if ($server == null && defined('DB_SERVER')) {
  222. $dsn = DB_TYPE . '://' . DB_USERNAME . ':' . rawurlencode(DB_PASSWORD) . '@' . rawurlencode(DB_SERVER) . '/' . DB_DATABASE;
  223. } else if ($server) {
  224. $dsn = DB_TYPE . '://' . $username . ':' . rawurlencode($password) . '@' . rawurlencode($server) . '/' . $database;
  225. }
  226. if (isset($dsn) && $dsn) {
  227. $_dba = @NewADOConnection($dsn);
  228. if (is_object($_dba)) {
  229. $_dba->setFetchMode(ADODB_FETCH_ASSOC);
  230. if (DB_CHARSET != '') {
  231. $names = 'SET NAMES \'' . DB_CHARSET . '\'';
  232. if (DB_COLLATE != '') {
  233. $names .= ' COLLATE \'' . DB_COLLATE . '\'';
  234. }
  235. $_dba->Execute($names);
  236. }
  237. ADOdb_Active_Record::SetDatabaseAdapter($_dba);
  238. } else if (defined('DB_SERVER')) {
  239. $v = View::getInstance();
  240. $v->renderError(t('Unable to connect to database.'), t('A database error occurred while processing this request.'));
  241. }
  242. } else {
  243. return false;
  244. }
  245. }
  246. //$_dba->LogSQL(true);
  247. //global $ADODB_PERF_MIN;
  248. //$ADODB_PERF_MIN = 0;
  249. return $_dba;
  250. }
  251. /**
  252. * Loads a helper file. If the same helper file is contained in both the core concrete directory and the site's directory, it will load the site's first, which could then extend the core.
  253. */
  254. public function helper($file, $pkgHandle = false) {
  255. static $instances = array();
  256. $class = Object::camelcase($file) . "Helper";
  257. $siteclass = "Site" . Object::camelcase($file) . "Helper";
  258. if (array_key_exists($class, $instances)) {
  259. $instance = $instances[$class];
  260. } else if (array_key_exists($siteclass, $instances)) {
  261. $instance = $instances[$siteclass];
  262. } else {
  263. $env = Environment::get();
  264. $f1 = $env->getRecord(DIRNAME_HELPERS . '/' . $file . '.php', $pkgHandle);
  265. require_once($f1->file);
  266. if ($f1->override) {
  267. if (class_exists($siteclass, false)) {
  268. $class = $siteclass;
  269. }
  270. } else if ($pkgHandle) {
  271. $pkgclass = Object::camelcase($pkgHandle . '_' . $file) . "Helper";
  272. if (class_exists($pkgclass, false)) {
  273. $class = $pkgclass;
  274. }
  275. }
  276. $instances[$class] = new $class();
  277. $instance = $instances[$class];
  278. }
  279. if(method_exists($instance,'reset')) {
  280. $instance->reset();
  281. }
  282. return $instance;
  283. }
  284. /**
  285. * @access private
  286. */
  287. public function package($pkgHandle) {
  288. // loads and instantiates the object
  289. $env = Environment::get();
  290. $path = $env->getPath(FILENAME_PACKAGE_CONTROLLER, $pkgHandle);
  291. if (file_exists($path)) {
  292. require_once($path);
  293. }
  294. $class = Object::camelcase($pkgHandle) . "Package";
  295. if (class_exists($class)) {
  296. $cl = new $class;
  297. return $cl;
  298. }
  299. }
  300. /**
  301. * @access private
  302. */
  303. public function startingPointPackage($pkgHandle) {
  304. // loads and instantiates the object
  305. $dir = (is_dir(DIR_STARTING_POINT_PACKAGES . '/' . $pkgHandle)) ? DIR_STARTING_POINT_PACKAGES : DIR_STARTING_POINT_PACKAGES_CORE;
  306. if (file_exists($dir . '/' . $pkgHandle . '/' . FILENAME_PACKAGE_CONTROLLER)) {
  307. require_once($dir . '/' . $pkgHandle . '/' . FILENAME_PACKAGE_CONTROLLER);
  308. $class = Object::camelcase($pkgHandle) . "StartingPointPackage";
  309. if (class_exists($class)) {
  310. $cl = new $class;
  311. return $cl;
  312. }
  313. }
  314. }
  315. /**
  316. * Gets the path to a particular page type controller
  317. */
  318. public function pageTypeControllerPath($ctHandle) {
  319. self::model('collection_types');
  320. $ct = CollectionType::getByHandle($ctHandle);
  321. if (!is_object($ct)) {
  322. return false;
  323. }
  324. $pkgHandle = $ct->getPackageHandle();
  325. $env = Environment::get();
  326. $path = $env->getPath(DIRNAME_CONTROLLERS . '/' . DIRNAME_PAGE_TYPES . '/' . $ctHandle . '.php', $pkgHandle);
  327. if (file_exists($path)) {
  328. return $path;
  329. }
  330. }
  331. /**
  332. * Loads a controller for either a page or view
  333. */
  334. public function controller($item) {
  335. $include = false;
  336. if (is_string($item)) {
  337. $db = self::db();
  338. if (is_object($db)) {
  339. try {
  340. $_item = Page::getByPath($item);
  341. if ($_item->isError()) {
  342. $path = $item;
  343. } else {
  344. $item = $_item;
  345. }
  346. } catch(Exception $e) {
  347. $path = $item;
  348. }
  349. } else {
  350. $path = $item;
  351. }
  352. }
  353. if ($item instanceof Page) {
  354. $c = $item;
  355. if ($c->getCollectionTypeID() > 0) {
  356. $ctHandle = $c->getCollectionTypeHandle();
  357. $path = self::pageTypeControllerPath($ctHandle, $item->getPackageHandle());
  358. if ($path != false) {
  359. require_once($path);
  360. $class = Object::camelcase($ctHandle) . 'PageTypeController';
  361. }
  362. } else if ($c->isGeneratedCollection()) {
  363. $file = $c->getCollectionFilename();
  364. if ($file != '') {
  365. // strip off PHP suffix for the $path variable, which needs it gone
  366. if (strpos($file, '/' . FILENAME_COLLECTION_VIEW) !== false) {
  367. $path = substr($file, 0, strpos($file, '/'. FILENAME_COLLECTION_VIEW));
  368. } else {
  369. $path = substr($file, 0, strpos($file, '.php'));
  370. }
  371. }
  372. }
  373. } else if ($item instanceof Block || $item instanceof BlockType) {
  374. $class = Object::camelcase($item->getBlockTypeHandle()) . 'BlockController';
  375. if ($item instanceof BlockType) {
  376. $controller = new $class($item);
  377. }
  378. if ($item instanceof Block) {
  379. $c = $item->getBlockCollectionObject();
  380. }
  381. }
  382. $controllerFile = $path . '.php';
  383. if ($path != '') {
  384. $env = Environment::get();
  385. $pkgHandle = false;
  386. if (is_object($item)) {
  387. $pkgHandle = $item->getPackageHandle();
  388. }
  389. $f1 = $env->getPath(DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER, $pkgHandle);
  390. $f2 = $env->getPath(DIRNAME_CONTROLLERS . $controllerFile, $pkgHandle);
  391. if (file_exists($f2)) {
  392. $include = true;
  393. require_once($f2);
  394. } else if (file_exists($f1)) {
  395. $include = true;
  396. require_once($f1);
  397. }
  398. if ($include) {
  399. $class = Object::camelcase($path) . 'Controller';
  400. }
  401. }
  402. if (!isset($controller)) {
  403. if ($class && class_exists($class)) {
  404. // now we get just the filename for this guy, so we can extrapolate
  405. // what our controller is named
  406. $controller = new $class($item);
  407. } else {
  408. $controller = new Controller($item);
  409. }
  410. }
  411. if (isset($c) && is_object($c)) {
  412. $controller->setCollectionObject($c);
  413. }
  414. return $controller;
  415. }
  416. }