Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.27 KB | None | 0 0
  1. <?php //-->
  2. /*
  3. * This file is part a custom application package.
  4. * (c) 2011-2012 Openovate Labs
  5. */
  6.  
  7. require_once('eden.php');
  8.  
  9. /**
  10. * The starting point of every application call. If you are only
  11. * using the framework you can rename this function to whatever you
  12. * like.
  13. *
  14. */
  15. function app() {
  16. $class = App::i();
  17. $args = func_get_args();
  18. switch(func_num_args()) {
  19. case 1:
  20. return $class->getResponse($args[0]);
  21. case 2:
  22. return $class->getResponse($args[0], $args[1]);
  23. case 0:
  24. default:
  25. return $class;
  26. }
  27.  
  28. return $class;
  29. }
  30.  
  31. /**
  32. * Defines the starting point of every site call.
  33. * Starts laying out how classes and methods are handled.
  34. *
  35. * @package Eden
  36. * @category site
  37. */
  38. class App extends Eden {
  39. /* Constants
  40. -------------------------------*/
  41. /* Public Properties
  42. -------------------------------*/
  43. /* Protected Properties
  44. -------------------------------*/
  45. protected $_database = NULL;
  46. protected $_cache = NULL;
  47. protected $_registry = NULL;
  48. protected $_pages = array();
  49.  
  50. /* Private Properties
  51. -------------------------------*/
  52. /* Magic
  53. -------------------------------*/
  54. public static function i() {
  55. return self::_getSingleton(__CLASS__);
  56. }
  57.  
  58. public function __construct() {
  59. if(!self::$_active) {
  60. self::$_active = $this;
  61. }
  62.  
  63. $this->_root = dirname(__FILE__);
  64.  
  65. $this->setLoader();
  66.  
  67. //require registry
  68. $this->_registry = Eden_Loader::i(true)
  69. ->load('Eden_Registry')
  70. ->Eden_Registry();
  71. }
  72.  
  73. /* Public R/R Methods
  74. -------------------------------*/
  75. /**
  76. * Lets the framework handle exceptions.
  77. * This is useful in the case that you
  78. * use this framework on a server with
  79. * no xdebug installed.
  80. *
  81. * @param string|null the error type to report
  82. * @param bool|null true will turn debug on
  83. * @return this
  84. */
  85. public function setDebug($reporting = NULL, $default = NULL) {
  86. Eden_Error::i()->argument(1, 'int', 'null')->argument(2, 'bool', 'null');
  87.  
  88. Eden_Loader::i()
  89. ->load('Eden_Template')
  90. ->Eden_Error_Event()
  91. ->when(!is_null($reporting), 1)
  92. ->setReporting($reporting)
  93. ->when($default === true, 4)
  94. ->setErrorHandler()
  95. ->setExceptionHandler()
  96. ->listen('error', $this, 'error')
  97. ->listen('exception', $this, 'error')
  98. ->when($default === false, 4)
  99. ->releaseErrorHandler()
  100. ->releaseExceptionHandler()
  101. ->unlisten('error', $this, 'error')
  102. ->unlisten('exception', $this, 'error');
  103.  
  104. return $this;
  105. }
  106.  
  107. /**
  108. * Sets the application absolute paths
  109. * for later referencing
  110. *
  111. * @return this
  112. */
  113. public function setPaths(array $paths = array()) {
  114. $default = array(
  115. 'root' => $this->_root,
  116. 'plugs' => $this->_root.'/plugs',
  117. 'assets' => $this->_root.'/assets',
  118. 'cache' => $this->_root.'/app/cache',
  119. 'page' => $this->_root.'/app/page');
  120.  
  121. //foreach default path
  122. foreach($default as $key => $path) {
  123. $this->_registry->set('path', $key, $path);
  124. }
  125.  
  126. //for each path
  127. foreach($paths as $key => $path) {
  128. //make them absolute
  129. $path = (string) Eden_Path::i($path)->absolute();
  130. //set it
  131. $this->_registry->set('path', $key, $path);
  132. }
  133.  
  134. return $this;
  135. }
  136.  
  137. /**
  138. * Sets page aliases
  139. *
  140. * @return this
  141. */
  142. public function setPages($pages, $absolute = false) {
  143. App_Error::i()
  144. ->argument(1, 'string', 'array')
  145. ->argument(2, 'bool');
  146.  
  147. if(is_string($pages)) {
  148. if(!$absolute) {
  149. $pages = $this->_registry->get('path', 'config').'/'.$pages;
  150. }
  151.  
  152. $pages = include($pages);
  153. }
  154.  
  155. $this->_pages = $pages;
  156. return $this;
  157. }
  158.  
  159. /**
  160. * returns the response
  161. *
  162. * @param EdenRegistry|null the request object
  163. * @return string
  164. */
  165. public function getResponse($path = '/', $post = NULL, $default = 'App_Page_Index') {
  166. global $_GET, $_POST;
  167.  
  168. //set the request
  169. $request = $this->_registry;
  170.  
  171. $path = str_replace('app://com.openovate.work', '', $path);
  172.  
  173. $_GET = array();
  174. if(strpos($path, '?') !== false) {
  175. $params = substr($path, strpos($path, '?')+1);
  176.  
  177. if(trim($params)) {
  178. parse_str($params, $_GET);
  179. }
  180.  
  181. $path = substr($path, 0, strpos($path, '?'));
  182. }
  183.  
  184. $_POST = array();
  185. if(is_array($post)) {
  186. $_POST = $post;
  187. } else if(is_string($post) && trim($post)) {
  188. parse_str($post, $_POST);
  189. }
  190.  
  191. //fix the request path
  192. $path = (string) Eden_Path::i($path);
  193.  
  194. //get the path array
  195. $pathArray = explode('/', $path);
  196.  
  197.  
  198. //determine the page suggestions
  199. $suggestions = array();
  200. //for each of the page pattern to paths
  201. foreach($this->_pages as $pagePattern => $pagePath) {
  202. //we need to replace % with .*
  203. //so for example /page/%/edit is the same as /page/.*/edit
  204. $pattern = '%'.str_replace('%', '.*', $pagePattern).'%';
  205. //if the path matches the response path key
  206. if(preg_match($pattern, $path)) {
  207. //add the path and the length to the suggestions
  208. //we do this because we need to sort the suggestions
  209. //by relavancy
  210. $suggestions[strlen($pagePattern)][] = array($pagePattern, $pagePath);
  211. }
  212. }
  213.  
  214. //by default the page is the home page
  215. $page = NULL;
  216. //when given a path like /page/1/edit
  217. //and a pattern like /page/%/edit
  218. //we can determine one of the path variables is 1
  219. //make a place holder for path variables
  220. //which we will find later
  221. $variables = array();
  222. //to do that we need suggestions
  223. //so if we have suggestions
  224. if(!empty($suggestions)) {
  225. //sort suggestions by length because the more detailed the
  226. //page link the more probable this is the page they actually want
  227. krsort($suggestions);
  228.  
  229. //for the page to fetch we only care about the first suggestion and
  230. //second index in array($pagePattern, $pagePath)
  231. $suggestions = array_shift($suggestions);
  232. $suggestions = array_shift($suggestions);
  233. $page = $suggestions[1];
  234.  
  235. //lets determine the path variables
  236. $variables = $this->_getPageVariables($path, $suggestions[0]);
  237. } else {
  238. $classBuffer = $pathArray;
  239. while(count($classBuffer) > 1) {
  240. $classParts = ucwords(implode(' ', $classBuffer));
  241. $page = 'App_Page'.str_replace(' ', '_', $classParts);
  242. if(class_exists($page)) {
  243. break;
  244. }
  245.  
  246. $variable = array_pop($classBuffer);
  247. array_unshift($variables, $variable);
  248. }
  249.  
  250. if(count($classBuffer) == 1) {
  251. $page = NULL;
  252. }
  253. }
  254.  
  255. $path = array(
  256. 'string' => $path,
  257. 'array' => $pathArray,
  258. 'variables' => $variables);
  259.  
  260. if(!isset($_FILES)) {
  261. $_FILES = array();
  262. }
  263.  
  264. //set the request
  265. $request->set('get', $_GET)
  266. ->set('post', $_POST)
  267. ->set('files', $_FILES)
  268. ->set('request', $path);
  269.  
  270. if(!$page || !class_exists($page)) {
  271. $page = $default;
  272. }
  273.  
  274. //set the response data
  275. try {
  276. return (string) $this->$page($request);
  277. } catch(Exception $e) {
  278. return $e->getMessage();
  279. }
  280. }
  281.  
  282. /* Public Database Methods
  283. -------------------------------*/
  284. /**
  285. * Sets up the default database connection
  286. *
  287. * @return this
  288. */
  289. public function addDatabase($key, $type = NULL,
  290. $host = NULL, $name = NULL,
  291. $user = NULL, $pass = NULL,
  292. $default = true) {
  293. App_Error::i()
  294. ->argument(1, 'string', 'array', 'null')
  295. ->argument(2, 'string', 'null')
  296. ->argument(3, 'string', 'null')
  297. ->argument(4, 'string', 'null')
  298. ->argument(5, 'string', 'null')
  299. ->argument(6, 'string', 'null')
  300. ->argument(7, 'bool');
  301.  
  302. if(is_array($key)) {
  303. $type = isset($key['type']) ? $key['type'] : NULL;
  304. $host = isset($key['host']) ? $key['host'] : NULL;
  305. $name = isset($key['name']) ? $key['name'] : NULL;
  306. $user = isset($key['user']) ? $key['user'] : NULL;
  307. $pass = isset($key['pass']) ? $key['pass'] : NULL;
  308. $default = isset($key['default']) ? $key['default'] : true;
  309. $key = isset($key['key']) ? $key['key'] : NULL;
  310. }
  311.  
  312. //connect to the data as described in the config
  313. switch($type) {
  314. case 'postgre':
  315. $database = Eden_Postgre::i($host, $name, $user, $pass);
  316. break;
  317. case 'mysql':
  318. $database = Eden_Mysql::i($host, $name, $user, $pass);
  319. break;
  320. case 'sqlite':
  321. $database = Eden_Sqlite::i($host);
  322. break;
  323. }
  324.  
  325. $this->_registry->set('database', $key, $database);
  326.  
  327. if($default) {
  328. $this->_database = $database;
  329. }
  330.  
  331. return $this;
  332. }
  333.  
  334. /**
  335. * Returns the default database instance
  336. *
  337. * @return Eden_Database_Abstract
  338. */
  339. public function getDatabase($key = NULL) {
  340. if(is_null($key)) {
  341. //return the default database
  342. return $this->_database;
  343. }
  344.  
  345. return $this->_registry->get('database', $key);
  346. }
  347.  
  348. /**
  349. * Sets the default database
  350. *
  351. * @param string key
  352. * @return this
  353. */
  354. public function setDefaultDatabase($key) {
  355. App_Error::i()->argument(1, 'string');
  356.  
  357. $args = func_get_args();
  358. //if the args are greater than 5
  359. //they mean to add it
  360. if(count($args) > 5) {
  361. $this->addDatabase($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
  362. }
  363.  
  364. //now set it
  365. $this->_database = $this->_registry->getValue('database', $key);
  366.  
  367. return $this;
  368. }
  369.  
  370. /* Public Event Methods
  371. -------------------------------*/
  372. /**
  373. * Starts filters. Filters will handle when to run.
  374. *
  375. * @param string|array handlers
  376. * @return Eden_Application
  377. */
  378. public function setFilters($filters) {
  379. App_Error::i()->argument(1, 'string', 'array');
  380.  
  381. if(is_string($filters)) {
  382. $filters = include($filters);
  383. }
  384.  
  385. //for each handler as class
  386. foreach($filters as $class) {
  387. //try to
  388. try {
  389. //instantiate the class
  390. $this->$class($this);
  391. //when there's an error do nothing
  392. } catch(Exception $e){}
  393. }
  394.  
  395. return $this;
  396. }
  397.  
  398. /* Public Misc Methods
  399. -------------------------------*/
  400. /**
  401. * Sets the cache
  402. *
  403. * @return this
  404. */
  405. public function setCache($root) {
  406. //we need Eden_Path to fix the path formatting
  407. if(!class_exists('Eden_Path')) {
  408. Eden_Loader::i()->load('Eden_Path');
  409. }
  410.  
  411. //format the path
  412. $root = (string) Eden_Path::i($root);
  413.  
  414. // Start the Global Cache
  415. Eden_Cache::i($root);
  416.  
  417. return $this;
  418. }
  419.  
  420. /**
  421. * Returns the current Registry
  422. *
  423. * @return Eden_Registry
  424. */
  425. public function getRegistry() {
  426. return $this->_registry;
  427. }
  428.  
  429. /**
  430. * Returns the template loaded with specified data
  431. *
  432. * @param array
  433. * @return Eden_Template
  434. */
  435. public function template($file, array $data = array()) {
  436. App_Error::i()->argument(1, 'string');
  437. return Eden_Template::i()->set($data)->parsePhp($file);
  438. }
  439.  
  440. /**
  441. * Error trigger output
  442. *
  443. * @return void
  444. */
  445. public function error($event, $type, $level, $class, $file, $line, $message, $trace, $offset) {
  446. $history = array();
  447. for(; isset($trace[$offset]); $offset++) {
  448. $row = $trace[$offset];
  449.  
  450. //lets formulate the method
  451. $method = $row['function'].'()';
  452. if(isset($row['class'])) {
  453. $method = $row['class'].'->'.$method;
  454. }
  455.  
  456. $rowLine = isset($row['line']) ? $row['line'] : 'N/A';
  457. $rowFile = isset($row['file']) ? $row['file'] : 'Virtual Call';
  458.  
  459. //add to history
  460. $history[] = array($method, $rowFile, $rowLine);
  461. }
  462.  
  463. echo Eden_Template::i()
  464. ->set('history', $history)
  465. ->set('type', $type)
  466. ->set('level', $level)
  467. ->set('class', $class)
  468. ->set('file', $file)
  469. ->set('line', $line)
  470. ->set('message', $message)
  471. ->parsePhp(dirname(__FILE__).'/front/error.phtml');
  472. }
  473.  
  474. /* Protected Methods
  475. -------------------------------*/
  476. protected function _getPageVariables($requestPath, $pagePath) {
  477. //if requestPath is not string
  478. if(!is_string($requestPath)) {
  479. //throw exception
  480. throw new Eden_Site_Exception(sprintf(Eden_Exception::NOT_STRING, 1));
  481. }
  482.  
  483. //if pagePath is not string
  484. if(!is_string($pagePath)) {
  485. //throw exception
  486. throw new Eden_Site_Exception(sprintf(Eden_Exception::NOT_STRING, 2));
  487. }
  488.  
  489. $variables = array();
  490.  
  491. //if the request path equals /
  492. if($requestPath == '/') {
  493. //there would be no page variables
  494. return array();
  495. }
  496.  
  497. //get the arrays
  498. $requestPathArray = explode('/', $requestPath);
  499. $pagePathArray = explode('/', $pagePath);
  500.  
  501. //we do not need the first path because
  502. // /page/1 is [null,page,1] in an array
  503. array_shift($requestPathArray);
  504. array_shift($pagePathArray);
  505.  
  506. //for each request path
  507. foreach($requestPathArray as $i => $value) {
  508. //if the page path is not set, is null or is '%'
  509. if(!isset($pagePathArray[$i])
  510. || trim($pagePathArray[$i]) == NULL
  511. || $pagePathArray[$i] == '%') {
  512. //then we can assume it's a variable
  513. $variables[] = $requestPathArray[$i];
  514. }
  515. }
  516.  
  517. return $variables;
  518. }
  519.  
  520. /* Private Methods
  521. -------------------------------*/
  522. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement