1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15:
16: define('DS', DIRECTORY_SEPARATOR);
17:
18: 19: 20:
21: define('JPATH_BASE', dirname(__DIR__));
22:
23:
24: require JPATH_BASE . DS . 'core' . DS . 'libraries' . DS . 'exception.php';
25:
26: require JPATH_BASE . DS . 'core' . DS . 'libraries' . DS . 'autoloader.php';
27:
28: require JPATH_BASE . DS . 'app' . DS . 'bootstrap.php';
29:
30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42: class joosCore
43: {
44: 45: 46: 47: 48:
49: private static $is_admin = false;
50:
51: 52: 53: 54: 55: 56: 57: 58:
59: public static function user()
60: {
61: return self::$is_admin ? joosCoreAdmin::user() : modelUsers::instance();
62: }
63:
64: public static function set_admin_mode()
65: {
66: self::$is_admin = TRUE;
67: }
68:
69: public static function is_admin()
70: {
71: return (bool) self::$is_admin;
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public static function path($name, $type, $cat = '')
84: {
85: (JDEBUG && $name != 'debug') ? joosDebug::inc(sprintf('joosCore::%s - <b>%s</b>', $type, $name)) : null;
86:
87: switch ($type) {
88: case 'controller':
89: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'controller.' . $name . '.php';
90: break;
91:
92: case 'admin_controller':
93: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'controller.admin.' . $name . '.php';
94: break;
95:
96: case 'ajax_controller':
97: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'controller.' . $name . '.ajax.php';
98: break;
99:
100: case 'model':
101: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'models' . DS . 'model.' . $name . '.php';
102: break;
103:
104: case 'admin_model':
105: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'models' . DS . 'model.admin.' . $name . '.php';
106: break;
107:
108: case 'view':
109: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'views' . DS . $cat . DS . 'default.php';
110: break;
111:
112: case 'admin_view':
113: $file = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $name . DS . 'views_admin' . DS . $cat . DS . 'default.php';
114: break;
115:
116: case 'admin_template_html':
117: $file = JPATH_BASE . DS . 'app' . DS . 'templates' . DS . JTEMPLATE . DS . 'html' . DS . $name . '.php';
118: break;
119:
120: case 'module_helper':
121: $file = JPATH_BASE . DS . 'app' . DS . 'modules' . DS . $name . DS . 'helper.' . $name . '.php';
122: break;
123:
124: case 'module_admin_helper':
125: $file = JPATH_BASE . DS . 'app' . DS . 'modules' . DS . $name . DS . 'helper.' . $name . '.php';
126: break;
127:
128: case 'lib':
129: $file = JPATH_BASE . DS . 'core' . DS . 'libraries' . DS . $name . '.php';
130: break;
131:
132: case 'lib-vendor':
133: $file = JPATH_BASE . DS . 'app' . DS . 'vendors' . DS . $cat . DS . $name . DS . $name . '.php';
134: break;
135:
136: default:
137: throw new joosCoreException('Не найдено определние для типа файла :file_type', array(':file_type' => $type));
138: break;
139: }
140:
141: if (JDEBUG && !joosFile::exists($file)) {
142: throw new joosCoreException('Не найден требуемый файл :file для типа :name', array(':file' => $file, ':name' => ($cat ? sprintf('%s ( %s )', $name, $type) : $name)));
143: }
144:
145: return $file;
146: }
147:
148: }
149:
150: 151: 152: 153: 154:
155: class joosDocument
156: {
157: private static $instance;
158: public static $page_body;
159: public static $data = array('title' => array(), 'meta' => array(), 'custom' => array(),
160: 'js_files' => array(),
161: 'js_code' => array(), 'js_onready' => array(), 'css' => array(), 'header' => array(), 'pathway' => array(), 'pagetitle' => false, 'page_body' => false, 'html_body' => false, 'footer' => array(),);
162: public static $config = array('favicon' => true, 'seotag' => true,);
163: public static $seotag = array('distribution' => 'global', 'rating' => 'General', 'document-state' => 'Dynamic', 'documentType' => 'WebDocument', 'audience' => 'all', 'revisit' => '5 days', 'revisit-after' => '5 days', 'allow-search' => 'yes', 'language' => 'russian', 'robots' => 'index, follow');
164:
165: public static $cache_header_time = false;
166:
167: private function __construct()
168: {
169: }
170:
171: 172: 173: 174:
175: public static function instance()
176: {
177: if (self::$instance === null) {
178: self::$instance = new self;
179: self::$data['title'] = array(joosConfig::get2('info', 'title'));
180: }
181:
182: return self::$instance;
183: }
184:
185: public static function get_data($name)
186: {
187: return isset(self::$data[$name]) ? self::$data[$name] : false;
188: }
189:
190: public static function set_body($body)
191: {
192: self::$data['page_body'] = $body;
193: }
194:
195: public static function get_body()
196: {
197: return self::$data['page_body'];
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207:
208: public function set_page_title($title = '', $pagetitle = '')
209: {
210:
211: $title = $title ? $title : joosConfig::get2('info', 'title');
212: self::$data['title'] = array($title);
213:
214:
215: self::$data['pagetitle'] = $pagetitle ? $pagetitle : $title;
216:
217: return $this;
218: }
219:
220: 221: 222: 223: 224: 225: 226:
227: public function add_title($title = '')
228: {
229: self::$data['title'][] = $title;
230: }
231:
232: 233: 234: 235:
236: public static function get_title()
237: {
238: $title = array_reverse(self::$data['title']);
239:
240: return implode(' / ', $title);
241: }
242:
243: public function add_meta_tag($name, $content)
244: {
245: $name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
246: $content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
247: self::$data['meta'][] = array($name, $content);
248:
249: return $this;
250: }
251:
252: public function append_meta_tag($name, $content)
253: {
254: $n = count(self::$data['meta']);
255: for ($i = 0; $i < $n; $i++) {
256: if (self::$data['meta'][$i][0] == $name) {
257: $content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
258: if ($content != '' & self::$data['meta'][$i][1] == "") {
259: self::$data['meta'][$i][1] .= ' ' . $content;
260: }
261: ;
262:
263: return;
264: }
265: }
266:
267: $this->add_meta_tag($name, $content);
268: }
269:
270: public function prepend_meta_tag($name, $content)
271: {
272: $name = joosString::trim(htmlspecialchars($name, ENT_QUOTES, 'UTF-8'));
273: $n = count(self::$data['meta']);
274: for ($i = 0; $i < $n; $i++) {
275: if (self::$data['meta'][$i][0] == $name) {
276: $content = joosString::trim(htmlspecialchars($content, ENT_QUOTES, 'UTF-8'));
277: self::$data['meta'][$i][1] = $content . self::$data['meta'][$i][1];
278:
279: return;
280: }
281: }
282: self::instance()->add_meta_tag($name, $content);
283: }
284:
285: public function add_custom_head_tag($html)
286: {
287: self::$data['custom'][] = trim($html);
288:
289: return $this;
290: }
291:
292: public function add_custom_footer_tag($html)
293: {
294: self::$data['custom'][] = trim($html);
295:
296: return $this;
297: }
298:
299: public function get_head()
300: {
301: $head = array();
302: $head[] = isset(self::$data['title']) ? "\t" . '<title>' . self::get_title() . '</title>' : false;
303:
304: foreach (self::$data['meta'] as $meta) {
305: $head[] = '<meta name="' . $meta[0] . '" content="' . $meta[1] . '" />';
306: }
307:
308: foreach (self::$data['custom'] as $html) {
309: $head[] = $html;
310: }
311:
312: return implode("\n\t", $head) . "\n";
313: }
314:
315: 316: 317: 318: 319: 320: 321: 322:
323: public function add_js_file($path, $params = array('first' => false))
324: {
325: if (isset($params['first']) && $params['first'] == true) {
326: array_unshift(self::$data['js_files'], $path);
327: } else {
328: self::$data['js_files'][] = $path;
329: }
330:
331: 332:
333:
334: return $this;
335: }
336:
337: public function add_js_code($code)
338: {
339: self::$data['js_code'][] = $code;
340:
341: return $this;
342: }
343:
344: public function add_js_vars($code)
345: {
346: self::$data['js_vars'][] = $code;
347:
348: return $this;
349: }
350:
351: public function add_css($path, $params = array('media' => 'all'))
352: {
353: self::$data['css'][] = array($path, $params);
354:
355: return $this;
356: }
357:
358: public function seo_tag($name, $value)
359: {
360: self::$seotag[$name] = $value;
361:
362: return $this;
363: }
364:
365: public static function javascript()
366: {
367: return self::js_files() . self::js_code();
368: }
369:
370: 371: 372: 373:
374: public static function js_files()
375: {
376:
377: if (joosConfig::get2('cache', 'js_cache')) {
378: $js_file = joosJSOptimizer::optimize_and_save(self::$data['js_files']);
379:
380: return joosHtml::js_file($js_file['live'] . (JDEBUG ? '?' . time() : JFILE_ANTICACHE));
381: }
382:
383:
384: else {
385: $result = array();
386: foreach (self::$data['js_files'] as $js_file) {
387: $result[] = joosHtml::js_file($js_file . (JDEBUG ? '?' . time() : false));
388: }
389:
390: return implode("\n\t", $result) . "\n";
391: }
392: }
393:
394: public static function js_code()
395: {
396: $c = array();
397: foreach (self::$data['js_code'] as $js_code) {
398: $c[] = $js_code . ";\n";
399: }
400: $result = joosHtml::js_code(implode("", $c));
401:
402: return $result;
403: }
404:
405: public static function stylesheet()
406: {
407: $result = array();
408:
409: foreach (self::$data['css'] as $css_file) {
410:
411: $result[] = joosHtml::css_file($css_file[0] . (JDEBUG ? '?' . time() : JFILE_ANTICACHE), $css_file[1]['media']);
412: }
413:
414: return implode("\n\t", $result) . "\n";
415: }
416:
417: public static function head()
418: {
419: $jdocument = self::instance();
420:
421: $meta = joosDocument::get_data('meta');
422: $n = count($meta);
423:
424: $description = $keywords = false;
425:
426: for ($i = 0; $i < $n; $i++) {
427: if ($meta[$i][0] == 'keywords') {
428: $keywords = $meta[$i][1];
429: } else {
430: if ($meta[$i][0] == 'description') {
431: $description = $meta[$i][1];
432: }
433: }
434: }
435:
436: $description ? null : $jdocument->append_meta_tag('description', joosConfig::get2('info', 'description'));
437: $keywords ? null : $jdocument->append_meta_tag('keywords', joosConfig::get2('info', 'keywords'));
438:
439: if (joosDocument::$config['seotag'] == true) {
440: foreach (self::$seotag as $key => $value) {
441: $value != false ? $jdocument->add_meta_tag($key, $value) : null;
442: }
443: }
444:
445: echo $jdocument->get_head();
446:
447:
448: if (self::$config['favicon'] == true) {
449: $icon = JPATH_SITE . '/media/favicon.ico?v=2';
450: echo "\t" . '<link rel="shortcut icon" href="' . $icon . '" />' . "\n\t";
451: }
452: }
453:
454: public static function body()
455: {
456: echo self::$data['page_body'];
457: }
458:
459: public static function footer_data()
460: {
461: return implode("\n", self::$data['footer']);
462: }
463:
464: public static function head_data()
465: {
466: return implode("\n", self::$data['header']);
467: }
468:
469: public static function header()
470: {
471: if (!headers_sent()) {
472: if (self::$cache_header_time) {
473: header_remove('Pragma');
474: joosRequest::send_headers('Cache-Control: max-age=' . self::$cache_header_time);
475: joosRequest::send_headers('Expires: ' . gmdate('r', time() + self::$cache_header_time));
476: } else {
477: joosRequest::send_headers('Pragma: no-cache');
478: joosRequest::send_headers('Cache-Control: no-cache, must-revalidate');
479: }
480: joosRequest::send_headers('X-Powered-By: Joostina CMS');
481: joosRequest::send_headers('Content-type: text/html; charset=UTF-8');
482: }
483: }
484:
485: }
486:
487: 488: 489: 490: 491: 492: 493:
494: class joosController
495: {
496: public static $activroute;
497: public static $controller;
498: public static $task;
499: public static $param;
500: public static $error = false;
501:
502: public static function init()
503: {
504: joosDocument::header();
505: joosRoute::route();
506:
507: $events_name = 'core.init';
508: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name) : null;
509: }
510:
511: 512: 513: 514:
515: public static function run()
516: {
517:
518: if (self::$activroute != 'static_run') {
519: self::init();
520: }
521:
522: $events_name = 'core.run';
523: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name) : null;
524:
525: ob_start();
526:
527: $class = 'actions' . ucfirst(self::$controller);
528:
529: JDEBUG ? joosDebug::add($class . '::' . self::$task) : null;
530:
531: 532: 533: 534: 535:
536: if (method_exists($class, self::$task)) {
537:
538: $events_name = sprintf('controller.*');
539: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name, $class, self::$task) : null;
540:
541: $events_name = sprintf('controller.%s.*', $class);
542: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name, $class, self::$task) : null;
543:
544: $events_name = sprintf('controller.%s.%s', $class, self::$task);
545: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name, self::$param) : null;
546:
547:
548: method_exists($class, 'action_before') ? call_user_func_array($class . '::action_before', array(self::$task)) : null;
549:
550: $results = call_user_func($class . '::' . self::$task);
551:
552:
553: method_exists($class, 'action_after') ? call_user_func_array($class . '::action_after', array(self::$task, $results)) : null;
554:
555: if (is_array($results)) {
556: self::views($results, self::$controller, self::$task);
557: } elseif (is_string($results)) {
558: echo $results;
559: }
560:
561:
562: joosDocument::set_body(ob_get_clean());
563:
564: if (self::$activroute == 'static_run') {
565: return joosDocument::get_body();
566: }
567: } else {
568:
569: joosPages::page404('Метод не найден');
570: }
571: }
572:
573: public static function render()
574: {
575: ob_start();
576:
577: require_once (JPATH_BASE . '/app/templates/' . JTEMPLATE . '/index.php');
578:
579: return ob_get_clean();
580: }
581:
582: 583: 584: 585: 586:
587: public static function ajax_run()
588: {
589: joosRoute::init();
590:
591: $events_name = 'core.run';
592: joosEvents::has_events($events_name) ? joosEvents::fire_events($events_name) : null;
593:
594: $class = 'actionsAjax' . ucfirst(self::$controller);
595:
596: JDEBUG ? joosDebug::add($class . '::' . self::$task) : null;
597:
598: if (method_exists($class, self::$task)) {
599:
600:
601: method_exists($class, 'action_before') ? call_user_func_array($class . '::action_before', array(self::$task)) : null;
602:
603: $result = call_user_func($class . '::' . self::$task);
604:
605: method_exists($class, 'action_after') ? call_user_func_array($class . '::action_after', array(self::$task, $result)) : null;
606: } else {
607:
608: return self::ajax_error404();
609: }
610: if (is_array($result)) {
611: echo json_encode($result);
612: } elseif (is_string($result)) {
613: echo $result;
614: }
615: }
616:
617: private static function views(array $params, $option, $task)
618: {
619:
620: joosModule::init();
621: joosModule::set_controller_data($params);
622:
623: self::as_html($params, $option, $task);
624: }
625:
626: private static function as_html(array $params, $controller, $method)
627: {
628: $template = isset($params['template']) ? $params['template'] : 'default';
629: $view = isset($params['view']) ? $params['view'] : $method;
630:
631: extract($params, EXTR_OVERWRITE);
632: $viewfile = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $controller . DS . 'views' . DS . $view . DS . $template . '.php';
633:
634: joosFile::exists($viewfile) ? require ($viewfile) : null;
635: }
636:
637: public static function ajax_error404()
638: {
639: joosRequest::send_headers_by_code(404);
640: echo '404';
641:
642: self::$error = 404;
643:
644: return;
645: }
646:
647: 648: 649: 650: 651: 652: 653:
654: public static function static_run($controller, $task, array $params = array())
655: {
656: self::$controller = $controller;
657: self::$task = $task;
658: self::$param = $params;
659: self::$activroute = 'static_run';
660:
661: return self::run();
662: }
663:
664: 665: 666: 667: 668: 669: 670: 671: 672: 673:
674: public static function get_view($controller, $task, $template = 'default', $params = array())
675: {
676: extract($params, EXTR_OVERWRITE);
677: $viewfile = JPATH_BASE . DS . 'app' . DS . 'components' . DS . $controller . DS . 'views' . DS . $task . DS . $template . '.php';
678: joosFile::exists($viewfile) ? require ($viewfile) : null;
679: }
680:
681: public static function debug($sysstart)
682: {
683:
684:
685:
686: if (defined('_JOOS_MEM_USAGE')) {
687:
688: $mem_usage = ( memory_get_usage() - _JOOS_MEM_USAGE );
689: $mem_usage = joosFile::convert_size($mem_usage);
690: } else {
691: $mem_usage = 'Недоступно';
692: }
693:
694:
695: joosDebug::add_top(round(( microtime(true) - $sysstart), 5));
696: joosDebug::add_top($mem_usage);
697:
698:
699: joosDebug::get();
700: }
701:
702: }
703:
704: 705: 706: 707: 708: 709: 710:
711: class joosControllerAjax extends joosController
712: {
713: }
714:
715: 716: 717: 718:
719: function _xdump($var)
720: {
721: joosDebug::dump($var);
722: }
723:
724: 725: 726: 727:
728: class joosCoreException extends joosException
729: {
730: }
731: