├── .gitignore ├── README.md ├── app ├── Bootstrap.php ├── controllers │ ├── Admin.php │ ├── Application.php │ ├── Backend │ │ ├── Backend.php │ │ ├── Index.php │ │ └── Posts.php │ ├── Error.php │ ├── Index.php │ └── Restfull.php ├── models │ └── Post.php ├── modules │ └── Admin │ │ ├── _init.php │ │ ├── config │ │ └── routes.ini │ │ ├── controllers │ │ ├── Index.php │ │ ├── Pages.php │ │ └── Posts.php │ │ └── views │ │ ├── index │ │ └── index.phtml │ │ ├── layouts │ │ └── admin.phtml │ │ ├── pages │ │ └── index.phtml │ │ └── posts │ │ └── index.phtml ├── plugins │ ├── AuthToken.php │ └── Log.php └── views │ ├── application │ └── notfound.phtml │ ├── backend │ ├── index │ │ └── index.phtml │ └── posts │ │ └── index.phtml │ ├── error │ └── error.phtml │ ├── index │ └── index.phtml │ ├── layouts │ ├── backend.phtml │ └── frontend.phtml │ └── restfull │ ├── edit.phtml │ ├── form.phtml │ ├── index.phtml │ ├── new.phtml │ └── show.phtml ├── config ├── application.ini.default └── routing.ini ├── lib ├── Helper │ └── Html.php ├── Lycan │ └── Validations │ │ ├── Errors.php │ │ ├── ValidatableInterface.php │ │ ├── Validate.php │ │ ├── Validator.php │ │ └── Validators │ │ ├── Acceptance.php │ │ ├── Clusivity.php │ │ ├── Confirmation.php │ │ ├── Each.php │ │ ├── Email.php │ │ ├── Exclusion.php │ │ ├── Format.php │ │ ├── Inclusion.php │ │ ├── Length.php │ │ ├── Numericality.php │ │ └── Presence.php └── eYaf │ ├── Layout.php │ ├── Logger.php │ └── Request.php ├── log └── .gitignore ├── public ├── .htaccess ├── css │ ├── admin │ │ └── style.css │ └── style.css ├── img │ └── ap.jpg ├── index.php └── js │ └── app.js └── vendor └── .gitignore /.gitignore: -------------------------------------------------------------------------------- 1 | log/* 2 | tests/* 3 | config/application.ini 4 | public/favicon.ico 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A base application for Yaf framework 2 | 3 | Yaf framework documentation can be found at [php.net](http://www.php.net/manual/en/book.yaf.php) 4 | 5 | ## Requirements 6 | 7 | * Yaf php extension. Download and install from [Pecl](http://pecl.php.net/package/yaf) 8 | * PHP 5.3+ 9 | * Mysql server 10 | * Apache, Nginx or Lighttpd web server. 11 | * mod_rewrite and .htaccess enabled for Apache web server. 12 | 13 | ## Configuration 14 | 15 | * Info about setting up a server for Yaf can be found [here](http://www.php.net/manual/en/yaf.examples.php) 16 | * Rename `config/application.ini.default` to `config/application.ini` 17 | * If you have PHP 5.4 you can use the internal web server to test the project. 18 | * `cd yaf_base_application/public` 19 | * `php -S localhost:8000` 20 | * This project uses PHP 5.3 namespaces so `yaf.use_namespace` should be turned on. 21 | 22 | ## Additions 23 | 24 | * ~~A simple ORM database layer `lib/Orm`. Yaf Models extend `lib/Orm/Entity` class. (More documentation soon at wiki)~~ 25 | * Validation library `lib/Validations` from [another project](https://github.com/akDeveloper/Lycan) of mine for validating classes. 26 | * A Layout class that allows to render views inside a base html layout `lib/Layout.php`. Layouts directory can be defined in application.ini 27 | * A Logger class `lib/Logger.php` and a `LoggerPlugin` to log info about requests and database queries. (Make sure that log directory is readable.) 28 | * A custom Request class `lib/Request.php` that extends `Yaf\Request\Http` and offers input filter for request params, posts and queries. 29 | * ~~A Paginator `lib/Paginator` forked from Laravel framework and adjust it to work with `lib/Orm`~~ 30 | * An Authenticity token plugin `AuthTokenPlugin`to prevent Cross-site request forgery (csrf). Can be turned on/off from application.ini 31 | * A base `ApplicationController` which adds some base functionality like 404 not found page. 32 | * A `RestfullController` to make easy crud (create, read, update, delete) actions. 33 | * An `ErrorController` to catch all exceptions and display a page with error info and bugtrace. 34 | * Custom error_handler to catch errors and throws Exceptions. 35 | * Custom _init.php file for modules for extra configuration. 36 | * Some base helper classes `lib/Helper` 37 | -------------------------------------------------------------------------------- /app/Bootstrap.php: -------------------------------------------------------------------------------- 1 | setErrorHandler(array(get_class($this),'error_handler')); 14 | } 15 | 16 | public function _initConfig(Yaf\Dispatcher $dispatcher) 17 | { 18 | $this->config = Yaf\Application::app()->getConfig(); 19 | } 20 | 21 | public function _initRequest(Yaf\Dispatcher $dispatcher) 22 | { 23 | $dispatcher->setRequest(new Request()); 24 | } 25 | 26 | public function _initDatabase(Yaf\Dispatcher $dispatcher) 27 | { 28 | 29 | } 30 | 31 | public function _initPlugins(Yaf\Dispatcher $dispatcher) 32 | { 33 | $dispatcher->registerPlugin(new LogPlugin()); 34 | 35 | $this->config->application->protect_from_csrf && 36 | $dispatcher->registerPlugin(new AuthTokenPlugin()); 37 | 38 | } 39 | 40 | public function _initLoader(Yaf\Dispatcher $dispatcher) 41 | { 42 | } 43 | 44 | public function _initRoute(Yaf\Dispatcher $dispatcher) 45 | { 46 | $config = new Yaf\Config\Ini(APP_PATH . '/config/routing.ini'); 47 | $dispatcher->getRouter()->addConfig($config); 48 | } 49 | 50 | /** 51 | * Custom init file for modules. 52 | * 53 | * Allows to load extra settings per module, like routes etc. 54 | */ 55 | public function _initModules(Yaf\Dispatcher $dispatcher) 56 | { 57 | $app = $dispatcher->getApplication(); 58 | 59 | $modules = $app->getModules(); 60 | foreach ($modules as $module) { 61 | if ('index' == strtolower($module)) continue; 62 | 63 | require_once $app->getAppDirectory() . "/modules" . "/$module" . "/_init.php"; 64 | } 65 | } 66 | 67 | public function _initLayout(Yaf\Dispatcher $dispatcher) 68 | { 69 | $layout = new Layout($this->config->application->layout->directory); 70 | $dispatcher->setView($layout); 71 | } 72 | 73 | /** 74 | * Custom error handler. 75 | * 76 | * Catches all errors (not exceptions) and creates an ErrorException. 77 | * ErrorException then can caught by Yaf\ErrorController. 78 | * 79 | * @param integer $errno the error number. 80 | * @param string $errstr the error message. 81 | * @param string $errfile the file where error occured. 82 | * @param integer $errline the line of the file where error occured. 83 | * 84 | * @throws ErrorException 85 | */ 86 | public static function error_handler($errno, $errstr, $errfile, $errline) 87 | { 88 | // Do not throw exception if error was prepended by @ 89 | // 90 | // See {@link http://www.php.net/set_error_handler} 91 | // 92 | // error_reporting() settings will have no effect and your error handler 93 | // will be called regardless - however you are still able to read 94 | // the current value of error_reporting and act appropriately. 95 | // Of particular note is that this value will be 0 96 | // if the statement that caused the error was prepended 97 | // by the @ error-control operator. 98 | // 99 | if (error_reporting() === 0) return; 100 | 101 | throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/controllers/Admin.php: -------------------------------------------------------------------------------- 1 | getView()->setLayout($this->layout); 50 | 51 | //Set session. 52 | $this->session = Yaf\Session::getInstance(); 53 | 54 | // Assign session to views too. 55 | $this->getView()->session = $this->session; 56 | 57 | // Assign application config file to this controller 58 | $this->config = Yaf\Application::app()->getConfig(); 59 | 60 | // Assign config file to views 61 | $this->getView()->config = $this->config; 62 | } 63 | 64 | /** 65 | * When assign a public property to controller, this property will be 66 | * available to action view template too. 67 | * 68 | * @param string $name the name of the property 69 | * @param mixed $value the value of the property 70 | * 71 | * @return void 72 | */ 73 | public function __set($name, $value) 74 | { 75 | $this->$name = $value; 76 | $this->getView()->assignRef($name, $value); 77 | } 78 | 79 | public function getConfig() 80 | { 81 | return $this->config; 82 | } 83 | 84 | /** 85 | * Cancel current action proccess and forward to {@link notFound()} method. 86 | * 87 | * @return false 88 | */ 89 | public function forwardTo404() 90 | { 91 | $this->forward('Index','application','notFound'); 92 | $this->getView()->setScriptPath($this->getConfig()->application->directory 93 | . "/views"); 94 | header('HTTP/1.0 404 Not Found'); 95 | return false; 96 | } 97 | 98 | /** 99 | * Renders a 404 Not Found template view 100 | * 101 | * @return void 102 | */ 103 | public function notFoundAction() 104 | { 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /app/controllers/Backend/Backend.php: -------------------------------------------------------------------------------- 1 | getView()->setLayout(null); 8 | 9 | // fallback views path to global when error occured in modules. 10 | $config = Yaf\Application::app()->getConfig(); 11 | $this->getView()->setScriptPath($config->application->directory 12 | . "/views"); 13 | 14 | $this->getView()->e = $exception; 15 | $this->getView()->e_class = get_class($exception); 16 | $this->getView()->e_string_trace = $exception->getTraceAsString(); 17 | 18 | $params = $this->getRequest()->getParams(); 19 | unset($params['exception']); 20 | $this->getView()->params = array_merge( 21 | array(), 22 | $params, 23 | $this->getRequest()->getPost(), 24 | $this->getRequest()->getQuery() 25 | ); 26 | 27 | eYaf\Logger::getLogger()->logException($exception); 28 | 29 | switch ($exception->getCode()) { 30 | case YAF\ERR\AUTOLOAD_FAILED: 31 | case YAF\ERR\NOTFOUND\MODULE: 32 | case YAF\ERR\NOTFOUND\CONTROLLER: 33 | case YAF\ERR\NOTFOUND\ACTION: 34 | header('HTTP/1.1 404 Not Found'); 35 | break; 36 | case 401: 37 | $this->forward('Index','application','accessDenied'); 38 | header('HTTP/1.1 401 Unauthorized'); 39 | Yaf\Dispatcher::getInstance()->disableView(); 40 | echo $this->render('accessdenied'); 41 | break; 42 | default: 43 | header("HTTP/1.1 500 Internal Server Error"); 44 | break; 45 | } 46 | 47 | eYaf\Logger::stopLogging(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/controllers/Index.php: -------------------------------------------------------------------------------- 1 | heading = 'Home Page'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /app/controllers/Restfull.php: -------------------------------------------------------------------------------- 1 | getResourceCollection(); 34 | $this->getView()->assign('collection', $collection); 35 | } 36 | 37 | /** 38 | * Loads and displays a single resource asccording to Request::getParams() 39 | * values. 40 | * 41 | * By default the uri to show a resource is /module/controller/show/id/1 42 | * 43 | * @param int $id The id of resource to load 44 | */ 45 | public function showAction($id) 46 | { 47 | $resource = $this->getResource($id); 48 | 49 | if (null === $resource) { 50 | return $this->forwardTo404(); 51 | } else { 52 | $this->getView()->assign('resource', $resource); 53 | } 54 | } 55 | 56 | public function newAction() 57 | { 58 | $model = $this->get_model_name(); 59 | $resource = new $model(); 60 | $this->get_index_url(); 61 | $this->getView()->assign('resource', $resource); 62 | } 63 | 64 | public function createAction() 65 | { 66 | $model = $this->get_model_name(); 67 | 68 | $resource = new $model($this->get_resource_params($model)); 69 | 70 | if ($resource->save()) { 71 | $this->redirect($this->get_index_url()); 72 | return false; 73 | } else { 74 | Yaf\Dispatcher::getInstance()->disableView(); 75 | echo $this->render('new', array('resource'=>$resource, 76 | 'index_url'=>$this->get_index_url())); 77 | return false; 78 | } 79 | } 80 | 81 | public function editAction($id) 82 | { 83 | 84 | $resource = $this->getResource($id); 85 | 86 | if (null === $resource) { 87 | return $this->forwardTo404(); 88 | } else { 89 | $this->get_index_url(); 90 | $this->getView()->assign('resource', $resource); 91 | } 92 | } 93 | 94 | public function updateAction($id) 95 | { 96 | $resource = $this->getResource($id); 97 | 98 | if (null === $resource) { 99 | return $this->forwardTo404(); 100 | } 101 | 102 | $model = $this->get_model_name(); 103 | 104 | if ($resource->updateAttributes($this->get_resource_params($model))) { 105 | $this->redirect($this->get_index_url()); 106 | return false; 107 | } else { 108 | Yaf\Dispatcher::getInstance()->disableView(); 109 | echo $this->render('edit', array('resource'=>$resource, 110 | 'index_url'=>$this->get_index_url())); 111 | return false; 112 | } 113 | } 114 | 115 | public function deleteAction() 116 | { 117 | $resource = $this->getResource(); 118 | 119 | if (null === $resource) { 120 | return $this->forwardTo404(); 121 | } 122 | 123 | $this->resource->destroy(); 124 | 125 | $this->redirect($this->get_index_url()); 126 | return false; 127 | } 128 | 129 | /** 130 | * Creates a resource collection. 131 | * 132 | * By default it finds all elements of the resource. You can overload this 133 | * methods to return custom queries of the resource like pagination and/or 134 | * search. 135 | * 136 | * @return Orm\Mysql\Collection A collection object handling elements of 137 | * the resource 138 | */ 139 | public function getResourceCollection() 140 | { 141 | $model = $this->get_model_name(); 142 | 143 | return $model::find()->fetchAll(); 144 | } 145 | 146 | /** 147 | * Creates a resource by a given id. 148 | * 149 | * By default it finds the resource for the given id. 150 | * Overload this method if you want to load a resource with additional 151 | * criteria. 152 | * 153 | * @param int $id The id of resource to load. 154 | */ 155 | public function getResource($id) 156 | { 157 | if (null === $this->_resource) { 158 | $model = $this->get_model_name(); 159 | $this->_resource = $model::findById($id) 160 | ->fetch(); 161 | } 162 | return $this->_resource; 163 | } 164 | 165 | protected function get_model_name() 166 | { 167 | return Inflect::singularize($this->getRequest()->getControllerName()) 168 | . "Model"; 169 | } 170 | 171 | protected function get_resource_params($model) 172 | { 173 | $param_name = Inflect::underscore(str_replace('Model', '', $model)); 174 | $post = $this->getRequest()->getPost(); 175 | 176 | return $post[$param_name]; 177 | } 178 | 179 | protected function get_index_url() 180 | { 181 | 182 | if (null !== $this->_index_url) { 183 | return $this->_index_url; 184 | } 185 | 186 | $module = $this->getRequest()->getModuleName(); 187 | 188 | $this->_index_url = "/" 189 | . ('index' == strtolower($module) ? null : $module. "/") 190 | . Inflect::underscore($this->getRequest()->getControllerName()) 191 | . "/index"; 192 | 193 | $this->getView()->assign('index_url', $this->_index_url); 194 | return $this->_index_url; 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /app/models/Post.php: -------------------------------------------------------------------------------- 1 | getRouter()->addConfig($routes->admin); 7 | -------------------------------------------------------------------------------- /app/modules/Admin/config/routes.ini: -------------------------------------------------------------------------------- 1 | ;Admin routes 2 | admin.admin_index.type="rewrite" 3 | admin.admin_index.match="/(admin|admin/)$" 4 | admin.admin_index.route.module="admin" 5 | admin.admin_index.route.controller="index" 6 | admin.admin_index.route.action="index" 7 | -------------------------------------------------------------------------------- /app/modules/Admin/controllers/Index.php: -------------------------------------------------------------------------------- 1 | getView()->setLayoutPath( 12 | $this->getConfig()->application->directory 13 | . "/modules" . "/Admin" . "/views" . "/layouts" 14 | ); 15 | } 16 | 17 | public function indexAction() 18 | { 19 | $this->heading = "Dashboard"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/modules/Admin/controllers/Pages.php: -------------------------------------------------------------------------------- 1 | heading = 'Posts'; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /app/modules/Admin/views/index/index.phtml: -------------------------------------------------------------------------------- 1 | 2 |

3 | -------------------------------------------------------------------------------- /app/modules/Admin/views/layouts/admin.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Admin Panel 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/modules/Admin/views/pages/index.phtml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akDeveloper/yaf_base_application/225608db1c0a9bec158ba877df340aacf2821d75/app/modules/Admin/views/pages/index.phtml -------------------------------------------------------------------------------- /app/modules/Admin/views/posts/index.phtml: -------------------------------------------------------------------------------- 1 |

2 | -------------------------------------------------------------------------------- /app/plugins/AuthToken.php: -------------------------------------------------------------------------------- 1 | 10 | * application.protect_from_csrf=1 11 | * 12 | * 13 | * Then you must define an input hidden field in each html form you submit. 14 | * 15 | * 16 | * 17 | * 18 | * After submission of the form, the plugin will attempt to validate the 19 | * auth_token an will throw an \Exception if tokens are not equal. 20 | */ 21 | class AuthTokenPlugin extends Yaf\Plugin_Abstract 22 | { 23 | 24 | public function routerShutdown(Yaf\Request_Abstract $request , 25 | Yaf\Response_Abstract $response 26 | ) { 27 | $this->auth_token(); 28 | } 29 | 30 | public function dispatchLoopStartup(Yaf\Request_Abstract $request, 31 | Yaf\Response_Abstract $response 32 | ){ 33 | 34 | $this->verify_auth_token($request); 35 | } 36 | 37 | protected function verify_auth_token($request) 38 | { 39 | $config = Yaf\Application::app()->getConfig(); 40 | 41 | if ( $config['application']['protect_from_csrf'] 42 | && $request->isPost() 43 | ) { 44 | 45 | $post = $request->getPost(); 46 | 47 | if ( !isset($post['_auth_token']) 48 | || $post['_auth_token'] !== $this->auth_token() 49 | ){ 50 | throw new \Exception('Invalid authenticity token!'); 51 | } else { 52 | $session = Yaf\Session::getInstance(); 53 | $session->auth_token = NULL; 54 | $this->auth_token(); 55 | } 56 | } 57 | } 58 | 59 | /** 60 | * Creates a random token, ancodes it with Base64 and stores it to session 61 | * 62 | * @return string The authenticity token string. 63 | */ 64 | protected function auth_token() 65 | { 66 | $session = Yaf\Session::getInstance(); 67 | $session->auth_token = $session->auth_token 68 | ?: base64_encode(sha1(uniqid(rand(), true))); 69 | return $session->auth_token; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /app/plugins/Log.php: -------------------------------------------------------------------------------- 1 | log("[{$request->getRequestUri()}]"); 16 | } 17 | 18 | public function routerShutdown(Yaf\Request_Abstract $request, 19 | Yaf\Response_Abstract $response 20 | ){ 21 | Logger::getLogger()->logRequest($request); 22 | } 23 | 24 | public function dispatchLoopStartup(Yaf\Request_Abstract $request, 25 | Yaf\Response_Abstract $response 26 | ){ 27 | 28 | } 29 | 30 | public function preDispatch(Yaf\Request_Abstract $request , 31 | Yaf\Response_Abstract $response 32 | ){ 33 | 34 | } 35 | 36 | public function postDispatch(Yaf\Request_Abstract $request, 37 | Yaf\Response_Abstract $response 38 | ){ 39 | } 40 | 41 | public function dispatchLoopShutdown(Yaf\Request_Abstract $request, 42 | Yaf\Response_Abstract $response 43 | ){ 44 | Logger::stopLogging(); 45 | } 46 | 47 | 48 | public function preResponse(Yaf\Request_Abstract $request, 49 | Yaf\Response_Abstract $response 50 | ){ 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/views/application/notfound.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | The page you were looking for doesn't exist (404) 6 | 17 | 18 | 19 | 20 |
21 |

404: The page you were looking for doesn't exist.

22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /app/views/backend/index/index.phtml: -------------------------------------------------------------------------------- 1 | title = "Hello Backend!" ?> 2 |

title ?>

3 | -------------------------------------------------------------------------------- /app/views/backend/posts/index.phtml: -------------------------------------------------------------------------------- 1 | title = 'Posts' ?> 2 |

Posts Index

3 | -------------------------------------------------------------------------------- /app/views/error/error.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | <?php echo $e->getMessage() ?> 4 | 30 | 31 | 32 |

33 |
: getMessage()?> in file getFile()?> at line getLine()?>
34 | 35 |
36 |

Exception Trace

37 |
getTraceAsString()?>
38 |

Debug Backtrace

39 |

40 | 
41 | 
42 |
43 |

Request

44 |

45 | Parameters: 46 |

47 | 
48 |     
49 |

50 | 51 | 52 | -------------------------------------------------------------------------------- /app/views/index/index.phtml: -------------------------------------------------------------------------------- 1 | title = "Home";?> 2 |

3 | -------------------------------------------------------------------------------- /app/views/layouts/backend.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <?php echo $this->title?:null?> - Backend 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/views/layouts/frontend.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <?php echo $this->title?:null?> 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/views/restfull/edit.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/form.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/index.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/views/restfull/new.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/views/restfull/show.phtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /config/application.ini.default: -------------------------------------------------------------------------------- 1 | [yaf] 2 | ;APP_PATH is the constant defined in index.php 3 | application.directory=APP_PATH "/app" 4 | application.ext="php" 5 | application.view.ext="phtml" 6 | application.modules="Index,Admin" 7 | application.library=APP_PATH "/lib" 8 | application.library.directory=APP_PATH "/lib" 9 | application.library.namespace="" 10 | application.bootstrap=APP_PATH "/app" "/Bootstrap.php" 11 | application.baseUri="" 12 | application.dispatcher.defaultRoute="" 13 | application.dispatcher.throwException=1 14 | application.dispatcher.catchException=1 15 | application.dispatcher.defaultModule="index" 16 | application.dispatcher.defaultController="index" 17 | application.dispatcher.defaultAction="index" 18 | ;custom settings 19 | application.layout.directory=APP_PATH "/app" "/views" "/layouts" 20 | application.protect_from_csrf=1 21 | application.encoding=UTF-8 22 | ;product section inherit from yaf section 23 | [product:yaf] 24 | ; user configuartions list here 25 | database.mysql.host=localhost 26 | database.mysql.port=3306 27 | database.mysql.user= 28 | database.mysql.password= 29 | database.mysql.database= 30 | database.mysql.charset=utf8 31 | 32 | -------------------------------------------------------------------------------- /config/routing.ini: -------------------------------------------------------------------------------- 1 | ;Backend routes 2 | backend_index.type="rewrite" 3 | backend_index.match="/(backend|backend/)$" 4 | backend_index.route.module="index" 5 | backend_index.route.controller="backend_index" 6 | backend_index.route.action="index" 7 | backend_post_index.type="rewrite" 8 | backend_post_index.match="/backend/(posts|posts/)$" 9 | backend_post_index.route.module="index" 10 | backend_post_index.route.controller="backend_posts" 11 | backend_post_index.route.action="index" 12 | -------------------------------------------------------------------------------- /lib/Helper/Html.php: -------------------------------------------------------------------------------- 1 | '; 42 | } 43 | 44 | public static function linkTo($text, $url, $attrs = array()) 45 | { 46 | return '' . $text . ''; 47 | } 48 | 49 | public static function span($content, $attrs=array()) 50 | { 51 | return ''.self::encode($content).""; 52 | } 53 | 54 | public static function getEncoding() 55 | { 56 | if (self::$encoding) { 57 | return self::$encoding; 58 | } 59 | 60 | self::$encoding = 'utf-8'; 61 | 62 | return self::$encoding; 63 | } 64 | 65 | public static function validUrl($url) 66 | { 67 | if (null === $url || "" == $url) 68 | return null; 69 | 70 | return substr($url, 0, 4) == "http" ? $url : "http://" . $url; 71 | } 72 | 73 | public static function javascript($url) 74 | { 75 | return '' . PHP_EOL; 76 | } 77 | 78 | public static function css($url, $attrs=array()) 79 | { 80 | $defaults = array('media'=>'screen', 'type'=>'text/css', 'rel'=>'stylesheet'); 81 | 82 | $attrs = array_merge($attrs, $defaults); 83 | 84 | return '' . PHP_EOL; 85 | } 86 | 87 | /** 88 | * Appends a javascript file from current view. 89 | * 90 | * Some javascript files are required to load only in specific action 91 | * views. You can add them with this method. 92 | * 93 | * Notice that you have to add a line to your layout file like: 94 | * 95 | * javascripts ?> 96 | * 97 | * This is the place where additional javascript files would appear. 98 | * 99 | * @param View_Interface $view The view instance 100 | * @param string|array $args An array of javascript paths or a 101 | * string with javascript path to load. 102 | */ 103 | public static function appendJavascript(ViewInterface $view, $args) 104 | { 105 | if ( !isset($view->javascripts)) { 106 | $view->assign('javascripts', ''); 107 | } 108 | 109 | if ( !is_array($args)) { 110 | $args = array($args); 111 | } 112 | 113 | foreach ($args as $arg) { 114 | $url = strpos($arg, 'http') === 0 || strpos($arg, '/') === 0 115 | ? $arg 116 | : '/javascripts/'.$arg; 117 | $view->javascripts .= self::javascript($url . '.js'); 118 | } 119 | } 120 | 121 | /** 122 | * Appends a stylesheet file from current view. 123 | * 124 | * Some stylesheet files are required to load only in specific action 125 | * views. You can add them with this method. 126 | * 127 | * Notice that you have to add a line to your layout file like: 128 | * 129 | * css ?> 130 | * 131 | * This is the place where additional stylesheet files would appear. 132 | * 133 | * @param View_Interface $view The view instance 134 | * @param string|array $args An array of stylesheet paths or a 135 | * string with stylesheet path to load. 136 | */ 137 | public static function appendCss(ViewInterface $view, $args) 138 | { 139 | if ( !isset($view->stylesheets)) { 140 | $view->assign('stylesheets', ''); 141 | } 142 | 143 | if ( !is_array($args)) { 144 | $args = array($args); 145 | } 146 | 147 | foreach ($args as $arg) { 148 | $url = strpos($arg, 'http') === 0 || strpos($arg, '/') === 0 149 | ? $arg 150 | : '/stylesheets/'.$arg; 151 | $view->stylesheets .= self::css($url.'.css'); 152 | } 153 | } 154 | 155 | public static function attributes($array) 156 | { 157 | if (empty($array)) return null; 158 | 159 | $o = ""; 160 | foreach ($array as $k => $v) { 161 | if ( null !== $v ) 162 | $o .= ' ' . $k . '="' . self::encode($v) . '"'; 163 | } 164 | return ' '.$o; 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Errors.php: -------------------------------------------------------------------------------- 1 | getArrayCopy(); 17 | 18 | foreach ($array as $k=>$v) $this->offsetUnset($k); 19 | } 20 | 21 | public function isEmpty() 22 | { 23 | return $this->count() == 0; 24 | } 25 | 26 | public function contains($attribute) 27 | { 28 | return $this->offsetExists($attribute); 29 | } 30 | 31 | public function add($attribute, $message=null, $options=array()) 32 | { 33 | $message = $this->_normalize_message($attribute, $message, $options); 34 | !$this->offsetExists($attribute) ? $this->offsetSet($attribute, array()) : null; 35 | $val = $this->offsetGet($attribute); 36 | array_push($val, $message); 37 | $this->offsetSet($attribute, $val); 38 | } 39 | 40 | private function _normalize_message($attribute, $message, $options) 41 | { 42 | $message = $message ?: ':invalid'; 43 | 44 | if ( 0 === strpos($message, ':') ) { 45 | return $this->generateMessage($attribute, $message, $options); 46 | } elseif (is_callable($message)) { 47 | return $message(); 48 | } else { 49 | return $message; 50 | } 51 | } 52 | 53 | public function generateMessage($attribute, $type=':invalid', $options=array()) 54 | { 55 | $message = null; 56 | $type = substr($type, 1); 57 | 58 | if (isset($options['message'])) { 59 | if (0 === strpos($options['message'], ':')) 60 | $type = substr($options['message'], 1); 61 | else 62 | $message = $options['message']; 63 | } 64 | 65 | return $message ?: $type; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/ValidatableInterface.php: -------------------------------------------------------------------------------- 1 | 22 | * Array( 23 | * 'class_attribute_name' => Array( 24 | * [0] => 'Message', 25 | * [1] => 'Message', 26 | * ), 27 | * 'another_class_attribute_name' => Array( 28 | * [0] => 'Message', 29 | * ), 30 | * ) 31 | * 32 | * 33 | * @retrun array|ArrayIterator 34 | */ 35 | public function getErrors(); 36 | } 37 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validate.php: -------------------------------------------------------------------------------- 1 | 14 | * class MyClass 15 | * { 16 | * use Lycan\Validations\Validate; 17 | * 18 | * protected function validations() 19 | * { 20 | * $this->validates('property', array('Validator'=>$options); 21 | * } 22 | * 23 | * } 24 | * 25 | */ 26 | trait Validate 27 | { 28 | protected $default_keys = array('if', 'on', 'allow_empty', 'allow_null'); 29 | 30 | private $_errors; 31 | 32 | /** 33 | * Returns errors that occured after the validation of the class. 34 | * 35 | * @see \Lycan"validations\Errors 36 | * 37 | * @return \ArrayIterator 38 | */ 39 | public function errors() 40 | { 41 | $this->_errors = $this->_errors ?: new Errors(); 42 | return $this->_errors; 43 | } 44 | 45 | public function getErrors() 46 | { 47 | return $this->errors(); 48 | } 49 | 50 | /** 51 | * Checks if a class is valid 52 | * 53 | * @return boolean 54 | */ 55 | public function isValid() 56 | { 57 | $this->errors()->clear(); 58 | $this->run_validations(); 59 | return $this->errors()->isEmpty(); 60 | } 61 | 62 | /** 63 | * Checks if a class is invalid 64 | * 65 | * @return boolean 66 | */ 67 | public function isInvalid() 68 | { 69 | return !$this->isValid(); 70 | } 71 | 72 | /** 73 | * Validates properties of a class against validators. 74 | * 75 | * validations array must contain the name of Validator class without the 76 | * namespace and the options for this validator. 77 | * 78 | * 79 | * $validations = array('Validator' => array('message'=>'my error message')); 80 | * 81 | * $validations = array('Validator' => true); // you must pass true if 82 | * options are not exist. 83 | * 84 | * 85 | * On examples above the Lycan\Validations\Validators\Validator will be 86 | * called. 87 | * 88 | * @params string|array $attrs the properties of class to validate 89 | * @params array $validations an array of Validator name class and 90 | * its options. 91 | * 92 | * @throws \Exception if a Validator class does not exist. 93 | * 94 | * @return void 95 | */ 96 | final public function validates($attrs, array $validations) 97 | { 98 | foreach ($validations as $key=>$options) { 99 | 100 | $validator = "\\Lycan\\Validations\\Validators\\" . $key; 101 | 102 | if (!class_exists($validator)) { 103 | throw new \Exception("Unknown validator: {$key}"); 104 | } 105 | 106 | $defaults = $this->_parse_validates_options($options); 107 | $defaults['attributes'] = $attrs; 108 | $vtor = new $validator($defaults); 109 | $vtor->validate($this); 110 | } 111 | } 112 | 113 | public function readAttributeForValidation($attribute) 114 | { 115 | return isset($this->$attribute) ? $this->$attribute : null; 116 | } 117 | 118 | protected function validations() 119 | { 120 | return true; 121 | } 122 | 123 | protected function run_validations() 124 | { 125 | $this->validations(); 126 | 127 | return $this->errors()->count() == 0; 128 | } 129 | 130 | private function _parse_validates_options($options) 131 | { 132 | if (is_array($options)) { 133 | 134 | return $options; 135 | } elseif(is_bool($options)) { 136 | 137 | return array(); 138 | } else { 139 | 140 | return array('with' => $options); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validator.php: -------------------------------------------------------------------------------- 1 | options = $options; 15 | } 16 | 17 | public function kind() 18 | { 19 | if (null == $this->kind) { 20 | $name = explode('\\',get_class($this)); 21 | $this->kind = strtolower(array_pop($name)); 22 | } 23 | 24 | return $this->kind; 25 | } 26 | 27 | abstract public function validate($record); 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Acceptance.php: -------------------------------------------------------------------------------- 1 | 27 | * $options = array('Acceptance' => true); 28 | * $this->validates('terms_of_service', $options); 29 | * 30 | * $options = array('Acceptance' => array( 31 | * 'message'=> 'accept the terms or else ...', 32 | * 'if' => function($class){ 33 | * return !$class->isNeedToAccept; 34 | * } 35 | * )); 36 | * 37 | * $this->validates('terms_of_service',$options); 38 | * 39 | * 40 | * @vendor Lycan 41 | * @package Validations 42 | * @author Andreas Kollaros 43 | * @license MIT {@link http://opensource.org/licenses/mit-license.php} 44 | */ 45 | class Acceptance extends Each 46 | { 47 | protected $message = 'must be accepted'; 48 | 49 | public function __construct($options) 50 | { 51 | $options = array_merge($options, array('allow_null'=>true)); 52 | if (!isset($options['accept'])) { 53 | $options['accept'] = '1'; 54 | } 55 | parent::__construct($options); 56 | } 57 | 58 | public function validateEach($record, $attribute, $value) 59 | { 60 | if ($value != $this->options['accept']) { 61 | unset($this->options['allow_null']); 62 | unset($this->options['accept']); 63 | $record->errors()->add($attribute, $this->message, $this->options); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Clusivity.php: -------------------------------------------------------------------------------- 1 | options['in']; 12 | $exclusions = is_callable($delimiter) ? $delimiter($record) : $delimiter; 13 | 14 | return in_array($value, $exclusions); 15 | } 16 | 17 | protected function check_validity() 18 | { 19 | if (!is_array($this->options['in']) && !is_callable($this->options['in'])) 20 | throw new \InvalidArgumentException("`in` must be an array or a function"); 21 | } 22 | 23 | protected function filtered_options($value) 24 | { 25 | $options = array_diff_key($this->options, array('in'=>null)); 26 | $options['value'] = $value; 27 | return $options; 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Confirmation.php: -------------------------------------------------------------------------------- 1 | 17 | * @license MIT {@link http://opensource.org/licenses/mit-license.php} 18 | */ 19 | class Confirmation extends \Lycan\Validations\Validators\Each 20 | { 21 | public function validateEach($record, $attribute, $value) 22 | { 23 | $confirmed = $record->readAttributeForValidation( 24 | "{$attribute}_confirmation" 25 | ); 26 | 27 | if ($value != $confirmed) { 28 | $record->errors()->add($attribute, ':confirmation', $this->options); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Each.php: -------------------------------------------------------------------------------- 1 | attributes = isset($options['attributes']) 17 | ? ( 18 | !is_array($options['attributes']) 19 | ? array($options['attributes']) 20 | : $options['attributes'] 21 | ) 22 | : array(); 23 | 24 | if (empty($this->attributes)) { 25 | 26 | throw new \Exception('attributes cannot be empty'); 27 | } 28 | 29 | if (isset($options['attributes'])) { 30 | unset($options['attributes']); 31 | } 32 | 33 | parent::__construct($options); 34 | 35 | $this->check_validity(); 36 | } 37 | 38 | public function validate($record) 39 | { 40 | $if = $this->validates_if($record); 41 | if (false == $if) { 42 | 43 | return true; 44 | } 45 | 46 | foreach ($this->attributes as $attribute) { 47 | 48 | $value = $record->readAttributeForValidation($attribute); 49 | 50 | if ( (null === $value && isset($this->options['allow_null']) 51 | && true == $this->options['allow_null']) 52 | || (empty($value) && isset($this->options['allow_empty']) 53 | && true == $this->options['allow_empty']) 54 | ) { 55 | continue; 56 | } 57 | 58 | $this->validateEach($record, $attribute, $value); 59 | } 60 | } 61 | 62 | protected function validates_if($record) 63 | { 64 | if (isset($this->options['if'])) { 65 | $if = $this->options['if']; 66 | if (is_callable($if)) { 67 | return $if($record); 68 | } else if (method_exists($record, $if)) { 69 | return $record->$if(); 70 | } 71 | } 72 | return true; 73 | } 74 | 75 | abstract protected function validateEach($record, $attribute, $value); 76 | 77 | protected function check_validity() 78 | { 79 | 80 | } 81 | 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Email.php: -------------------------------------------------------------------------------- 1 | 20 | * @license MIT {@link http://opensource.org/licenses/mit-license.php} 21 | */ 22 | class Email extends Each 23 | { 24 | protected $message = 'is not a valid email'; 25 | 26 | protected function validateEach($record, $attribute, $value) 27 | { 28 | if (false === filter_var($value, FILTER_VALIDATE_EMAIL)) { 29 | $record->errors()->add($attribute, ':invalid', $this->options); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Exclusion.php: -------------------------------------------------------------------------------- 1 | is_include($record,$value)) { 12 | $record->errors()->add($attribute, ':exclusion',$this->filtered_options($value)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Format.php: -------------------------------------------------------------------------------- 1 | options['with'])) { 13 | 14 | $regexp = $this->_option_call($record, 'with'); 15 | if (!(preg_match($regexp, $value))) { 16 | $record->errors()->add( 17 | $attribute, 18 | ':invalid', 19 | array_merge($this->options, array('with'=>$value)) 20 | ); 21 | } 22 | } elseif (isset($this->options['without'])) { 23 | $regexp = $this->_option_call($record, 'without'); 24 | if (preg_match($regexp, $value)) 25 | $record->errors()->add( 26 | $attribute, 27 | ':invalid', 28 | array_merge($this->options, array('without'=>$value)) 29 | ); 30 | } 31 | } 32 | 33 | private function _option_call($record, $name) 34 | { 35 | $option = $this->options[$name]; 36 | return is_callable($option) ? $option($record) : $option; 37 | } 38 | 39 | protected function check_validity() 40 | { 41 | if (!(array_key_exists('with', $this->options) 42 | xor array_key_exists('without',$this->options)) 43 | ) { 44 | throw new \InvalidArgumentException('Either `with` or `without` must be supplied (but not both)'); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Inclusion.php: -------------------------------------------------------------------------------- 1 | is_include($record, $value)) { 12 | $record->errors()->add($attribute, ':inclusion', $this->filtered_options($value)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Length.php: -------------------------------------------------------------------------------- 1 | ':wrong_length', 'min' => ':too_short', 'max'=>':too_long'); 18 | protected $checks = array('is'=>'==', 'min'=>'>=', 'max' => '<='); 19 | 20 | private $_reserved_options = array('min'=>null, 'max'=>null, 'within'=>null, 'is'=>null, 'tokenizer'=>null, 'too_short'=>null, 'too_long'=>null); 21 | public function __construct($options) 22 | { 23 | 24 | $range = isset($options['in']) 25 | ? $options['in'] 26 | : (isset($options['within']) ? $options['within'] : null); 27 | if ($range) { 28 | if (!is_array($range)) 29 | throw new \InvalidArgumentException('`in` and `within` must be an array'); 30 | $options['min'] = min($range); 31 | $options['max'] = max($range); 32 | } 33 | 34 | parent::__construct($options); 35 | } 36 | 37 | public function validateEach($record, $attribute, $value) 38 | { 39 | $value = $this->_tokenize($value); 40 | $value_length = is_array($value) ? count($value) : strlen($value); 41 | 42 | foreach ($this->checks as $key => $operator) { 43 | 44 | if (!isset($this->options[$key])) continue; 45 | 46 | $check_value = $this->options[$key]; 47 | if ($this->_check_value($check_value, $value_length, $operator)) continue; 48 | 49 | $error_options = array_diff_key($this->options, $this->_reserved_options); 50 | 51 | $error_options['count'] = $check_value; 52 | $default_message = isset($this->options[$this->messages[$key]]) 53 | ? $this->options[$this->messages[$key]] 54 | : null; 55 | 56 | if ($default_message) 57 | $error_options['message'] = $error_options['message'] ?: $default_message; 58 | 59 | $record->errors()->add($attribute, $this->messages[$key], $error_options); 60 | } 61 | } 62 | 63 | private function _tokenize($value) 64 | { 65 | if (isset($this->options['tokenizer']) && is_string($value)) { 66 | $tokenizer = $this->options['tokenizer']; 67 | if (is_callable($tokenizer)) 68 | return $tokenizer($value); 69 | } 70 | return $value; 71 | } 72 | 73 | private function _check_value($check_value,$value_length, $operator) 74 | { 75 | switch ($operator) { 76 | case '==': 77 | return $value_length == $check_value; 78 | break; 79 | case '>=': 80 | return $value_length >= $check_value; 81 | break; 82 | case '<=': 83 | return $value_length <= $check_value; 84 | break; 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Numericality.php: -------------------------------------------------------------------------------- 1 | '>', 11 | 'greater_than_or_equal_to' => '>=', 12 | 'equal_to' => '==', 13 | 'less_than' => '==', 14 | 'less_than_or_equal_to' => '<=', 15 | 'odd' => 'odd', 16 | 'even' => 'even', 17 | 'other_than' => '!=' 18 | ); 19 | 20 | private $_reserved_options = array( 21 | 'greater_than' => '>', 22 | 'greater_than_or_equal_to' => '>=', 23 | 'equal_to' => '==', 24 | 'less_than' => '<', 25 | 'less_than_or_equal_to' => '<=', 26 | 'odd' => 'odd', 27 | 'even' => 'even', 28 | 'other_than' => '!=', 29 | 'only_integer' => null 30 | ); 31 | 32 | protected function validateEach($record, $attribute, $value) 33 | { 34 | if (array_key_exists('allow_null',$this->options)) { 35 | if ($this->options['allow_null'] && null === $value) 36 | return; 37 | } 38 | 39 | if (array_key_exists('only_integer',$this->options)) { 40 | if ($this->options['only_integer']) { 41 | if (!($value = $this->parse_value_as_integer($value))) { 42 | $record->errors()->add($attribute, ':not_an_integer', $this->filtered_options($value)); 43 | return; 44 | } 45 | } 46 | } 47 | $options = array_intersect_key($this->options, $this->checks); 48 | foreach ($options as $option => $option_value) { 49 | switch ($option) { 50 | case 'odd': 51 | if ( 1 !== (1 & $value)) 52 | $record->errors()->add($attribute, ":$option", $this->filtered_options($value)); 53 | break; 54 | case 'even': 55 | if ( 0 !== (1 & $value)) 56 | $record->errors()->add($attribute, ":$option", $this->filtered_options($value)); 57 | break; 58 | default: 59 | 60 | if ( is_callable($option_value)) $option_value = $option_value($record); 61 | 62 | if ( false === $this->_check_value($value, $option_value, $this->checks[$option])) { 63 | $o = $this->filtered_options($value); 64 | $o['count'] = $option_value; 65 | $record->errors()->add($attribute, ":$option", $o); 66 | } 67 | break; 68 | } 69 | } 70 | 71 | } 72 | 73 | protected function check_validity() 74 | { 75 | $options = array_intersect_key($this->options, $this->checks); 76 | foreach ($options as $option=>$value) { 77 | if ($option == 'odd' || $option == 'even' || is_numeric($value) || is_callable($value)) continue; 78 | throw new \InvalidArgumentException("{$option} must be a number or a function"); 79 | } 80 | } 81 | 82 | protected function parse_value_as_number($value) 83 | { 84 | if ( is_numeric($value)) { 85 | if ( is_float($value) ) return (float) $value; 86 | if ( is_int($value) ) return (int) $value; 87 | } 88 | } 89 | 90 | protected function parse_value_as_integer($value) 91 | { 92 | if ( is_numeric($value) && is_int($value)) 93 | return (int) $value; 94 | else 95 | return null; 96 | } 97 | 98 | protected function filtered_options($value) 99 | { 100 | $options = array_diff_key($this->options, $this->_reserved_options); 101 | $options['value'] = $value; 102 | return $options; 103 | } 104 | 105 | private function _check_value($record_value, $check_value, $operator) 106 | { 107 | switch ($operator) { 108 | case '>': 109 | return $record_value > $check_value; 110 | break; 111 | case '<': 112 | return $record_value < $check_value; 113 | break; 114 | case '==': 115 | return $record_value == $check_value; 116 | break; 117 | case '>=': 118 | return $record_value >= $check_value; 119 | break; 120 | case '<=': 121 | return $record_value <= $check_value; 122 | break; 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /lib/Lycan/Validations/Validators/Presence.php: -------------------------------------------------------------------------------- 1 | readAttributeForValidation($attribute); 12 | 13 | $is_empty = is_object($value) && method_exists($value, 'isEmpty') 14 | ? $value->isEmpty() 15 | : empty($value); 16 | 17 | if ($is_empty) { 18 | $record->errors()->add($attribute, ':not_empty', $this->options); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /lib/eYaf/Layout.php: -------------------------------------------------------------------------------- 1 | 17 | * application/views/layouts/front.phtml 18 | * 19 | * 20 | * 21 | * <?php echo $title ?> 22 | * 23 | * 24 | * 26 | * 27 | * 28 | * 29 | * 30 | * If no layout is defined then returns the renderd action view template. 31 | * 32 | * Also allows to set variable to views and access them from the base 33 | * skeleton layout. In above layout $title variable is set to title tag. 34 | * Then in a view template we can set the value for $title variable. 35 | * 36 | * application/views/index/index.phtml 37 | * 38 | * title = "Index Action" ?> 39 | *

Index Action

40 | *
41 | * 42 | * @author Andreas Kollaros 43 | * 44 | */ 45 | class Layout implements \Yaf\View_Interface 46 | { 47 | 48 | /** 49 | * The template engine to render views and layout templates. 50 | * 51 | * Default engine is Yaf\View\Simple 52 | * 53 | * @var Yaf\View\Simple 54 | */ 55 | public $engine; 56 | 57 | /** 58 | * Options to be passed to template engine. 59 | * 60 | * @var array 61 | */ 62 | protected $options=array(); 63 | 64 | /** 65 | * 66 | */ 67 | protected $layout_path; 68 | 69 | /** 70 | * The name of layout file without extension. 71 | * 72 | * @var string 73 | */ 74 | protected $layout; 75 | 76 | /** 77 | * Handles the rendered action view data. 78 | * 79 | * @var string 80 | */ 81 | protected $content; 82 | 83 | /** 84 | * Array with assigned template variables. 85 | * 86 | * @var array 87 | */ 88 | protected $tpl_vars = array(); 89 | 90 | /** 91 | * Template directory. 92 | * 93 | * @var string 94 | */ 95 | protected $tpl_dir; 96 | 97 | /** 98 | * Constructor 99 | * 100 | * @param array $options key/value pair of options to be assigned to 101 | * template engine. 102 | * 103 | * @return void 104 | */ 105 | public function __construct($path, $options=array()) 106 | { 107 | $this->layout_path = $path; 108 | $this->options = $options; 109 | } 110 | 111 | /** 112 | * Return the instance of a template engine. 113 | * 114 | * @return Yaf\View\Simple 115 | */ 116 | protected function engine() 117 | { 118 | $this->engine = $this->engine ?: new \Yaf\View\Simple( 119 | $this->tpl_dir, 120 | $this->options 121 | ); 122 | 123 | return $this->engine; 124 | } 125 | 126 | /** 127 | * Create engine instance and set the path of views and layout templates. 128 | * 129 | * Layout path is set by default to layout directory inside views path. 130 | * 131 | * @param string $path The directory to set as the path. 132 | * 133 | * @return void 134 | */ 135 | public function setScriptPath($path) 136 | { 137 | if (is_readable($path)) { 138 | $this->tpl_dir = $path; 139 | $this->engine()->setScriptPath($path); 140 | 141 | // Overwirte layouts path by setting it where views path is. 142 | // This will force layout in modules to be placed in 143 | // modules/views/layouts directory 144 | $this->layout_path = $path . "/layouts"; 145 | 146 | return true; 147 | } 148 | 149 | throw new \Exception("Invalid path: {$path}"); 150 | } 151 | 152 | /** 153 | * Getter method for views path. 154 | * 155 | * @return string 156 | */ 157 | public function getScriptPath() 158 | { 159 | return $this->engine()->getScriptPath(); 160 | } 161 | 162 | /** 163 | * Setter for Layout::layout variable 164 | * 165 | * @param string $name the name of layout file without extension. 166 | * 167 | * @return void 168 | */ 169 | public function setLayout($name) 170 | { 171 | $this->layout = $name; 172 | } 173 | 174 | /** 175 | * Getter for Layout::layout variable 176 | * 177 | * @return string the name of layout file without extension 178 | */ 179 | public function getLayout() 180 | { 181 | return $this->layout; 182 | } 183 | 184 | 185 | public function setLayoutPath($path) 186 | { 187 | $this->layout_path = $path; 188 | } 189 | 190 | /** 191 | * Get full layout path with filename and extension. 192 | * 193 | * @return string 194 | */ 195 | public function getLayoutPath() 196 | { 197 | $config = \Yaf\Application::app()->getConfig()->get('application'); 198 | return $this->layout_path . "/" . $this->layout . ".{$config->view->ext}"; 199 | } 200 | 201 | /** 202 | * Assign a variable to the template 203 | * 204 | * @param string $name The variable name. 205 | * @param mixed $value The variable value. 206 | * 207 | * @return void 208 | */ 209 | public function __set($name, $value) 210 | { 211 | $this->assign($name, $value); 212 | } 213 | 214 | /** 215 | * Allows testing with empty() and isset() to work 216 | * 217 | * @param string $name 218 | * 219 | * @return boolean 220 | */ 221 | public function __isset($name) 222 | { 223 | return (null !== $this->engine()->$name); 224 | } 225 | 226 | /** 227 | * Allows unset() on object properties to work 228 | * 229 | * @param string $name 230 | * 231 | * @return void 232 | */ 233 | public function __unset($name) 234 | { 235 | $this->engine()->clear($name); 236 | } 237 | 238 | /** 239 | * Assign variables to the template 240 | * 241 | * Allows setting a specific key to the specified value, OR passing 242 | * an array of key => value pairs to set en masse. 243 | * 244 | * @see __set() 245 | * 246 | * @param string|array $name The assignment strategy to use (key or 247 | * array of key => value pairs) 248 | * @param mixed $value (Optional) If assigning a named variable, 249 | * use this as the value. 250 | * 251 | * @return void 252 | */ 253 | public function assign($name, $value = null) 254 | { 255 | 256 | $this->tpl_vars[$name] = $value; 257 | 258 | $this->engine()->assign($name, $value); 259 | } 260 | 261 | /** 262 | * Assign variables by reference to the template 263 | * 264 | */ 265 | public function assignRef($name, &$value) 266 | { 267 | $this->tpl_vars[$name] = $value; 268 | 269 | $this->engine()->assignRef($name, $value); 270 | } 271 | 272 | /** 273 | * Clear all assigned variables 274 | * 275 | * Clears all variables assigned to Yaf\View either via 276 | * {@link assign()} or property overloading 277 | * ({@link __get()}/{@link __set()}). 278 | * 279 | * @return void 280 | */ 281 | public function clearVars() { 282 | $this->tpl_vars = array(); 283 | $this->engine()->clear(); 284 | } 285 | 286 | /** 287 | * Processes a view and returns the output. 288 | * 289 | * This method called once from controller to render the given view. 290 | * So render the view at $this->content property and then render the 291 | * layout template. 292 | * 293 | * @param string $tpl The template to process. 294 | * @param array $tpl_vars Additional variables to be assigned to template. 295 | * 296 | * @return string The view or layout template output. 297 | */ 298 | public function render($tpl, $tpl_vars=array()) { 299 | 300 | $tpl_vars = array_merge($this->tpl_vars, $tpl_vars); 301 | 302 | $this->content = $this->engine()->render($tpl, $tpl_vars); 303 | 304 | // if no layout is defined, 305 | // return the rendered view template 306 | if (null == $this->layout) { 307 | 308 | return $this->content; 309 | } 310 | 311 | // If we assign some variables into view template, then maybe we need 312 | // them to layout view. 313 | // Hack?? 314 | // Get template vars from view via Reflection class and assign them to 315 | // layout view 316 | $ref = new \ReflectionClass($this->engine()); 317 | $prop = $ref->getProperty('_tpl_vars'); 318 | $prop->setAccessible(true); 319 | $view_vars = $prop->getValue($this->engine()); 320 | 321 | $tpl_vars = array_merge($tpl_vars, $view_vars); 322 | $tpl_vars['_content_'] = $this->content; 323 | 324 | return $this->engine()->render( 325 | $this->getLayoutPath(), 326 | $tpl_vars 327 | ); 328 | } 329 | 330 | /** 331 | * Directly display the constens of a view / layout template. 332 | * 333 | * @param string $tpl The template to process. 334 | * @param array $tpl_vars Additional variables to be assigned to template. 335 | * 336 | * @return void 337 | */ 338 | public function display($tpl, $tpl_vars=array()) { 339 | echo $this->render($tpl, $tpl_vars); 340 | } 341 | 342 | } 343 | -------------------------------------------------------------------------------- /lib/eYaf/Logger.php: -------------------------------------------------------------------------------- 1 | log($buffer); 35 | } 36 | 37 | public static function stopLogging() 38 | { 39 | $buffer = self::COLOR_SEQ . self::GREEN . "Completed in " 40 | . number_format((microtime(true) - self::$start_time) * 1000, 0) 41 | . "ms | " 42 | . "Mem Usage: (" 43 | . number_format( (memory_get_usage(true) - self::$memory) / (1024), 0, ",", "." ) 44 | ." kb)" 45 | . self::RESET_SEQ; 46 | static::getLogger()->log($buffer); 47 | } 48 | 49 | public static function getLogger($env=null, $open_mode="a") 50 | { 51 | if (static::$logger_instance) return static::$logger_instance; 52 | $env = $env ?: \Yaf\ENVIRON; 53 | $filename = APP_PATH . '/log' . DS . $env . '.log'; 54 | static::$logger_instance = new static($filename,$open_mode); 55 | return static::$logger_instance; 56 | } 57 | 58 | public function __construct($filename=null, $open_mode = "a") 59 | { 60 | $filename = $filename ?: APP_PATH . "/log" . DS . \Yaf\ENVIRON . ".log"; 61 | parent::__construct($filename, $open_mode); 62 | } 63 | 64 | public function log($string) 65 | { 66 | $this->fwrite($string . "\n"); 67 | } 68 | 69 | public function errorLog($string) 70 | { 71 | $this->log(self::COLOR_SEQ . "1;37m" . "!! WARNING: " . $string . self::RESET_SEQ); 72 | } 73 | 74 | public function logQuery($query, $class_name=null, $parse_time = 0, $action='Load') 75 | { 76 | $class_name = $class_name ?: 'Sql'; 77 | $buffer = self::COLOR_SEQ . self::PURPLE . "$class_name $action (" 78 | . number_format($parse_time * 1000, '4') 79 | . "ms) " . self::RESET_SEQ . self::COLOR_SEQ . self::WHITE 80 | . $query . self::RESET_SEQ; 81 | 82 | $this->log($buffer); 83 | } 84 | 85 | public function logRequest($request) 86 | { 87 | $this->log("Processing " 88 | . $request->getModuleName() .'\\' 89 | . $request->getControllerName() 90 | . "Controller#" 91 | . $request->getActionName() 92 | . " (for {$request->getServer('REMOTE_ADDR')}" 93 | . " at " . date('Y-m-d H:i:s') .")" 94 | . " [{$request->getMethod()}]" 95 | ); 96 | $params = array(); 97 | $params = array_merge($params, 98 | $request->getParams(), 99 | $request->getPost(), 100 | $request->getFiles(), 101 | $request->getQuery() 102 | ); 103 | $this->log("Parameters: " . print_r($params, true)); 104 | } 105 | 106 | public function logException($exception) 107 | { 108 | $this->log( 109 | get_class($exception) . ": " 110 | . $exception->getMessage() 111 | . " in file " 112 | . $exception->getFile() 113 | . " at line " 114 | . $exception->getLine() 115 | ); 116 | $this->log($exception->getTraceAsString()); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /lib/eYaf/Request.php: -------------------------------------------------------------------------------- 1 | _posts) { 24 | return $this->_posts; 25 | } 26 | 27 | $this->_posts = $this->filter_params(parent::getPost()); 28 | return $this->_posts; 29 | } 30 | 31 | public function getParams() 32 | { 33 | if ($this->_params) { 34 | return $this->_params; 35 | } 36 | 37 | $this->_params = $this->filter_params(parent::getParams()); 38 | return $this->_params; 39 | 40 | } 41 | 42 | public function getQuery() 43 | { 44 | if ($this->_query) { 45 | return $this->_query; 46 | } 47 | 48 | $this->_query = $this->filter_params(parent::getQuery()); 49 | return $this->_query; 50 | 51 | } 52 | 53 | private function filter_params($params) 54 | { 55 | if (!empty($params)) { 56 | array_walk_recursive($params, function(&$value, $key){ 57 | $value=htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); 58 | }); 59 | } 60 | 61 | return $params; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /log/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /public/.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteRule .* index.php 4 | -------------------------------------------------------------------------------- /public/css/admin/style.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akDeveloper/yaf_base_application/225608db1c0a9bec158ba877df340aacf2821d75/public/css/admin/style.css -------------------------------------------------------------------------------- /public/css/style.css: -------------------------------------------------------------------------------- 1 | footer { 2 | font-size: 70%;} 3 | -------------------------------------------------------------------------------- /public/img/ap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akDeveloper/yaf_base_application/225608db1c0a9bec158ba877df340aacf2821d75/public/img/ap.jpg -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | bootstrap() //call bootstrap methods defined in Bootstrap.php 7 | ->run(); 8 | ?> 9 | -------------------------------------------------------------------------------- /public/js/app.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akDeveloper/yaf_base_application/225608db1c0a9bec158ba877df340aacf2821d75/public/js/app.js -------------------------------------------------------------------------------- /vendor/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | --------------------------------------------------------------------------------