Warning, /webapps/ocs-server/gfx3/docs/manual.txt is written in an unsupported language. File is not indexed.

0001 ------------------------------------------------------------------------------
0002 |                         1 . Introduction                                   |
0003 ------------------------------------------------------------------------------
0004 
0005 This library is written for fun and quick development. So, also the user 
0006 manual tries to follow the main philosophy behind this framework.
0007 One of the most important tips you should never forget while coding with this
0008 lib is be lazy. You shouldn’t find yourself doing so much work for doing a small
0009 (or) repetitive job. Maybe you’re doing some mistakes somewhere. You should
0010 looking over standard classes and maybe consider deriving some of them.
0011 One other thing you should keep in mind is: this lib is built with many conven-
0012 tions in mind. If you keep them in mind, you should have no problems in fast
0013 coding. In the other case, you’ll better change framework.
0014 For now, that’s all folks! Let’s so exploring the standard website structure of a
0015 GFX installation.
0016 
0017 ------------------------------------------------------------------------------
0018 |                      2 . Creating index.php                                |
0019 ------------------------------------------------------------------------------
0020 
0021 Our index.php file must be placed at the root of our web site, it must just include
0022 gfx lib file so it will appear like this:
0023 
0024         <?php
0025         include("gfx3/lib.php");
0026         ?>
0027 
0028 If you place this file also in a sub directory of gfx3 eg.. there's /gfx3 and you
0029 put index.php into /src/index.php EModuleIncluder will search for the lib and include it
0030 automatically.
0031 
0032 ------------------------------------------------------------------------------
0033 |                            3 . Templating                                  |
0034 ------------------------------------------------------------------------------
0035 
0036 GFX4 uses a Model-View-Controller pattern that works with a rewrite engine.
0037 
0038 In the model-view-controller the code is splitted mainly in 3 semantic categories
0039 which are, of course:
0040  - Model, represented by EModel class
0041  - View, represented by simple html, css or js templating code
0042  - Controller, represented by a subclass of the EController class
0043 
0044 Each one of these should contain a different type of code. Let's see them closer.
0045 
0046 ------------------------------MODEL--------------------------------------
0047 
0048 The EModel class incorporates the old EData class now deprecated and contains
0049 various facilities for querying databases and performing standard tasks like SELECT,
0050 INSERT, UPDATE and DELETE. So, for example, if you're writing a class that interfaces
0051 with the "posts" class in your database, you should create a file name "posts.model.php"
0052 in the "model" directory. GFX already knows that there are models there.
0053 
0054 Every model should be name following this syntax <tablename>.model.php in order to be
0055 automatically loaded by gfx as a model. Then you can write your code that basically queries
0056 the database and returns the correct data, along with all manipulations done.
0057 
0058 Think of a Model of an object that has to return data that should already human understandable
0059 if seen raw.
0060 
0061 ------------------------------VIEW---------------------------------------
0062 
0063 Then there's the View folder, which is at the root of the gfx application and is called "views".
0064 This one contains php files named under the following syntax:
0065                 <viewname>.views.php
0066 Each view has the php final extension but can contain both php and standard templating code like
0067 pure html, css, js. In fact, a view should contain exaclty the code needed to display data to the
0068 user in the desired way. This will be achieved by a particular interaction that we'll see later
0069 in this guide.
0070 
0071 You can also create a views hierarchy simply by creating a set of folders. The load process
0072 will be completely transparent for the engine, and it's done by simply putting the entire url starting
0073 from "views" in the load command. This will be seen in a pratical example later.
0074 
0075 ------------------------------CONTROLLER---------------------------------
0076 
0077 Lastly, the more important of both the components, the Controller, represented by the EController
0078 class and located in the folder "controllers" following this filename syntax:
0079                 <controllername>.controller.php
0080 
0081 The controller is used to "glue" the data produced with the model and put it in the view in the correct
0082 order. In the controller will be written also all the logic of the application.
0083 
0084 Every controller class name should named after the "section" of the website that you want to handle.
0085 For example the MainController->index($args) above will be executed when www.mywebsite.com/main/index
0086 is called. 
0087 
0088 Here is a practical example of a working controller.
0089 
0090 ----------------------------main.controller.php------------------------------
0091 
0092 <?php
0093 class MainController extends EController
0094 {
0095         public function index($args)
0096         {
0097                 EStructure::view("home");
0098         }
0099         
0100         public function last_news()
0101         {
0102                 $articles = new ArticlesModel();
0103                 EStructure::view("articles", $articles->getAll());
0104         }
0105 }
0106 ?>
0107 
0108 ------------------------------articles.model.php-----------------------------
0109 <?php
0110 class ArticlesModel extends EModel
0111 {
0112         public function __construct()
0113         {
0114                 parent::__construct("articles");
0115         }
0116         
0117         public function getAll()
0118         {
0119                 $data = $this->find("*");
0120                 return $data;
0121         }
0122 }
0123 ?>
0124 -----------------------------articles.views.php------------------------------
0125 <html>
0126         <head><title>Articles page</title></head>
0127         <body>
0128                 <?php foreach($data as $article) { ?>
0129                 <h1><?php echo $article['id']; ?></h1>
0130                 <p><?php echo $article['text']; ?></p>
0131                 <?php } ?>
0132         </body>
0133 </html>
0134 
0135 MVC can be managed in the generic config file ("gfx3/config/generic.conf.php"):
0136 Example:
0137 <?php die("You cannot see config in here."); ?>
0138 mvc|yes
0139 mvc|no
0140 
0141 ------------------------------------------------------------------------------
0142 |                         4 . URL Rewriting                                  |
0143 ------------------------------------------------------------------------------
0144 Gfx contains a simple but working url rewriter that works in conjunction with
0145 the MVC system. It basically translates an URL asked by the client into a new one
0146 elaborated by the server.
0147 
0148 All the rules are in the file "gfx3/config/rewrite.conf.php". This is an example rewrite config file:
0149 
0150 <?php die(); ?>
0151 /help|/help/index
0152 /yourpath|/install/index
0153 
0154 Notice how the first URL is the one written by the client and the second one is that one elaborated
0155 by the server, when handling the request of a webpage.
0156 
0157 Everything that comes after a controller is usually handled as a parameter, except when you rewrite the URL.
0158 So, for example, calling /help/index/arg1/arg2/arg3 will be directly passed as an argument array to the method
0159 index($args) of the controller HelpController. Every other standard GET parameter can be passed as usual:
0160 http://www.example.com/help/index?x=5
0161 http://www.example.com/help/index/?x=5
0162 http://www.example.com/help/index/arg1/arg2?x=5
0163 http://www.example.com/help/index/arg1/arg2/?x=5
0164 
0165 In those URLs, the get parameters will be held in the EHeaderDataParser class, that offers also protection against
0166 SQL injections.
0167 
0168 URL rewriting can be managed in the generic config file ("gfx3/config/generic.conf.php"):
0169 Example:
0170 <?php die("You cannot see config in here."); ?>
0171 rewrite|yes
0172 rewrite|no
0173 
0174 ------------------------------------------------------------------------------
0175 |                         5 . User management                                |
0176 ------------------------------------------------------------------------------
0177 
0178 In GFX3 there's a static class called EUser. In order to have users working on your system just call
0179 
0180         EUser::load();
0181 
0182 Your database must be working and a db table called "users" must be set up with (at least) the following structure:
0183  
0184 +-----------+------------------+------+-----+---------+----------------+
0185 | Field     | Type             | Null | Key | Default | Extra          |
0186 +-----------+------------------+------+-----+---------+----------------+
0187 | id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
0188 | login     | varchar(45)      | NO   |     | NULL    |                |
0189 | password  | varchar(45)      | NO   |     | NULL    |                |
0190 | firstname | varchar(45)      | NO   |     | NULL    |                |
0191 | lastname  | varchar(45)      | NO   |     | NULL    |                |
0192 | email     | varchar(100)     | NO   |     | NULL    |                |
0193 | tgoup     | text             | NO   |     | NULL    |                |
0194 +-----------+------------------+------+-----+---------+----------------+
0195 
0196 And it will automatically do all the job. This is also OCS complaint, as for OCS v1.6.
0197 
0198 Small reference:
0199 
0200         EUser::login($login,$password) -> execute login if possible
0201         EUser::logged() -> returns true or false depending if logged or not
0202         EUser::logout() -> execute logout
0203         EUser::refresh() -> refresh current info in case you're dynamically modifying user data
0204         EUser::gdeny($group) -> deny the access to the webpage if user does belong to $group, allow others
0205         EUser::gallow($group) -> allow the access to the webpage if user does belong to $group, deny others
0206         EUser::belongs_to_group($group) -> returns boolen. True if user belongs to $group.
0207         
0208 Users can be managed in the generic config file ("gfx3/config/generic.conf.php"):
0209 Example:
0210 <?php die("You cannot see config in here."); ?>
0211 users|yes
0212 users|no
0213 
0214 ------------------------------------------------------------------------------
0215 |                    6 . Low-level MySQL database                            |
0216 ------------------------------------------------------------------------------
0217 
0218 In GFX3 there's the static class EDatabase which provide a direct communication
0219 channel with your database.
0220 It is automatically initialized and you simply have to call it.
0221 
0222 Small reference:
0223 
0224         EDatabase::q($sql_query) -> execute $sql_query
0225         EDatabase::sq($sql_query) -> execute $sql_query and returns the first result. Useful for counts etc. 
0226         EDatabase::table_exists($table) -> check if table exists and return boolean.
0227         EDatabase::last_insert_id() -> returns the id of the last inserted row.
0228 
0229 Database (both high and low level) can be managed in the generic config file ("gfx3/config/generic.conf.php"):
0230 Example:
0231 <?php die("You cannot see config in here."); ?>
0232 database|yes
0233 database|no
0234 
0235 ------------------------------------------------------------------------------
0236 |                      7 . High-level MySQL database                         |
0237 ------------------------------------------------------------------------------
0238 
0239 In GFX3 there's an abstraction layer up on mysql tables called EData.
0240 First, you prepare your EData object by creating an object like this:
0241 
0242         $dbtable = new EData("dbtable"); // "test" is the exact name of the table
0243 
0244 And then you can use all of EData methods. Small API:
0245 
0246         $dbtable->insert(array $allowed_fields) -> insert a new row 
0247         $dbtable->find($what, $where) -> returns the result of the query into an associative array example: $dbtable->find("*","id = 6");
0248         $dbtable->count($field, $where) -> returns an integer returned from a count query. example: $dbtable->count("id","id = 6");
0249         $dbtable->is_there($field,$where) -> check if $field is present $where.
0250         $dbtable->update($where, array $allowed_fields) -> update same as insert
0251         $dbtable->delete($where, $howmany) -> delete same as insert
0252 
0253 For some special methods like find, insert, update and delete EData will take
0254 $_POST data with the same name as the field name on the database table.
0255 Please be careful when using those intelligent features as they are easily
0256 exploitable. In order to avoid those kind of bugs always use $allowed_fields.
0257 
0258 ------------------------------------------------------------------------------
0259 |                          8 . Error Management                              |
0260 ------------------------------------------------------------------------------
0261 
0262 GFX3 provides a nice class to handle error management.
0263 
0264         ELog::error("this will be shown");
0265 
0266 This string will produce a backtrace and break the execution of the script exactly where this is introduced.
0267 This is the main difference between an error and a warning.
0268 
0269         ELog::warning("this will be shown");
0270 
0271 Error management can be suppressed and managed in the file "gfx3/config/generic.conf.php".
0272 
0273 <?php die("You cannot see config in here."); ?>
0274 
0275 errormode|normal                        -> Errors are shown as normal
0276 errormode|formatted                     -> Errors are shown with little html formatting
0277 errormode|file                          -> Errors are written in a log file accessible only from internal server
0278 errormode|suppressed            -> Errors are simply suppressed (suggested option in production environment)
0279 
0280 ------------------------------------------------------------------------------
0281 |                          9 . Debugging tools                               |
0282 ------------------------------------------------------------------------------
0283 
0284 The only debugging tool provided is a breakpoint.
0285 You can use it like this:
0286 
0287         eval(DEBUG_BREAKPOINT);
0288         
0289 This will also print the variable stack in which the breakpoint is called.
0290 
0291 ------------------------------------------------------------------------------
0292 |                     10 . Retrieving safe GET/POST data                      |
0293 ------------------------------------------------------------------------------
0294 
0295 GFX provides a class that automatically handles and parse GET and POST data.
0296 So instead of directly accessing those arrays you should use EHeaderDataParser.
0297 
0298 If you have to output your data you can use:
0299         
0300         EHeaderDataParser::out_get(string $name);
0301         EHeaderDataParser::out_post(string $name);
0302 
0303 If you have to insert your data into database and you need them to be safe you can use:
0304         
0305         EHeaderDataParser::db_get(string $name);
0306         EHeaderDataParser::db_post(string $name);
0307 
0308 If you need all your data to be safe you can easily do:
0309         
0310         EHeaderDataParser::safeAll();
0311 
0312 You can also set GET and POST manually using:
0313 
0314         EHeaderDataParser::add_get(string $key, string $value);
0315         EHeaderDataParser::add_post(string $key, string $value);
0316         
0317 You can use this class to get/set cookie data via:
0318         EHeaderDataParser::get_cookie(string $key);
0319         EHeaderDataParser::set_cookie(string $key, $value);
0320 
0321 This can result particularly useful when you need to use EModel's insert and update
0322 but not all the data comes from user request.
0323 
0324 If you want to take those variables fast and you can't decide you can choose
0325 the unsafe way using:
0326         
0327         EHeaderDataParser::get(string $key);
0328         EHeaderDataParser::post(string $key);
0329 
0330 If you have a string formed as a standard get key/value list (e.g. example=val&example2=val2)
0331 you can use this handy method to have that added to the module's stack.
0332         
0333         EHeaderDataParser::add_from_string(string $str);
0334 
0335 EHeaderDataParser can be configure in the generic config file of gfx, which can be located
0336 here: "gfx3/config/generic.conf.php".
0337 
0338 Example:
0339 <?php die("You cannot see config in here."); ?>
0340 protectheaders|yes
0341 protectheaders|no
0342         
0343 ------------------------------------------------------------------------------
0344 |                             11 . URL Rewriting                             |
0345 ------------------------------------------------------------------------------
0346 
0347 GFX has a module that do URL rewriting without any .htaccess stuff.
0348 You have to set your page to be rewritable under /config/rewrite.conf.php
0349 
0350 like this:
0351         
0352         <?php die(); ?>
0353         /|/main/index|exact
0354         /help|/help/index|normal
0355         /games|/games/lates|normal
0356 
0357 And then it will rewrite doing a simple string replace, but internally on the engine.
0358 So in this case /help will be rewritten internally as /help/index and therefore
0359 calling the index() method of the HelpController class defined.
0360 
0361 If more rules are matching for the same url, the one with the longest key will be
0362 used. Example:
0363 
0364         <?php die(); ?>
0365         /help/games|/help/games/latest|normal
0366         /help|/help/index|normal
0367 
0368 Only the second rule will be considered, even if it's defined before the second one.
0369 
0370 Every other parameter added after those slashes will be handled as parameter that will
0371 be passed to the controller's method via the $args array.
0372 
0373 Example of HelpController:
0374         class HelpController extends EController {
0375                 public function index($args){
0376                         var_dump($args);
0377                 }
0378         }
0379 
0380 Example of user browser opening the page www.example.com/help/index/arg1/arg2:
0381         {
0382                 "arg1",
0383                 "arg2"
0384         }
0385 
0386 Every other parameter passed as standard GET like ?var1=val1&var2=val2 is correctly
0387 set on EHeaderDataParser.
0388 
0389 This kind of parameter management works also for rewritten URLs. For example it would
0390 exactly be the same if the user calls: www.example.com/help/arg1/arg2
0391 without the "/index" part.
0392 
0393 This is the "normal" rewrite.
0394 
0395 The "exact" mode rewrites a URL only if it matches exactly so additional parameters via /
0396 aren't handled and accepted. Normal GET parameters are handled as usual. 
0397 
0398 In order to build a nice title (also if it's not useful for your app logic you should use:
0399         
0400         $url = ERewriter::prettify("I'm a terrible title");
0401 
0402 Rewriting can be enabled/disabled via general.conf.php:
0403         <?php die("You cannot see config in here."); ?>
0404         rewrite|yes
0405         rewrite|no
0406 ------------------------------------------------------------------------------
0407 |                            12 . Config manager                             |
0408 ------------------------------------------------------------------------------
0409 
0410 GFX use a class to load config every time from a bunch of files. You can also
0411 create your config and find them placed under EConfig.
0412 
0413 If you edit your /config/database.conf.php like this:
0414         <?php die(); ?>
0415         name|test
0416         host|localhost
0417         user|root
0418         password|asd
0419 
0420 You will automatically find them placed calling
0421         EConfig::$data["database"]["name"];
0422 
0423 Note that "database" is the filename without extensions and name is the variable name.
0424 Don't be afraid to use this feature as config files are cached using the cache engine.
0425 
0426 Also note that PHP code in config files will just be ignored, so it's quite safe to put
0427 some kind of die(); on the first line so no one will be able to see your inner config.
0428 
0429 You can also use multi value properties like this:
0430 --------------/config/myname.conf.php-------------
0431         <?php die(); ?>
0432         info|Claudio|Desideri|kde
0433 
0434 If you access EConfig::$data["myname"]["info"]; you get an array containing all the values.
0435 You can iterate on it using:
0436         foreach(EConfig::$data["myname"]["info"] as $item){
0437                 echo $item;
0438         }
0439 
0440 This will print:
0441         Claudio
0442         Desideri
0443         kde
0444 
0445 And via code that will be accessible using:
0446     $info = EConfig::$data["myname"]["info"];
0447     $info[0];
0448     $info[1];
0449     $info[2];
0450 
0451 ------------------------------------------------------------------------------
0452 |                           13 . Cache Engine                                |
0453 ------------------------------------------------------------------------------
0454 
0455 GFX make large use of APC module in order to be more efficient and caching everything 
0456 is cacheable.
0457 
0458 You can create your list of vars (like in EConfig) using ECacheVar. Small reference:
0459         $cvar = new ECacheVar("name of the container");
0460         $cvar->get($var) -> get previously setted variable
0461         $cvar->set($var, $value) -> set new variable
0462         $cvar->del($var) -> delete a variable
0463         $cvar->get_array_assoc(); -> returns an associative array of the whole container (iterable)
0464 
0465 You can create your full page of cache using ECacheList. Small reference:
0466         $cvar = new ECacheList("name of the container");
0467         $cvar->get() -> get previously setted value
0468         $cvar->set($value) -> set container value
0469         $cvar->del() -> delete the container
0470 
0471 It's also present an abstraction to put entire files into cache so you won't be loading too many times
0472 that txt file. Small referece:
0473         $cvar = new ECacheFile($filepath); -> load file from cache. If not present put into cache and load.
0474         $cvar->get(); -> get file value
0475 
0476 ------------------------------------------------------------------------------
0477 |                       14 . File System utilities                           |
0478 ------------------------------------------------------------------------------
0479 
0480 GFX offers a static class as a file system helper. Small reference:
0481         EFileSystem::get_file_extension(string $filename) -> returns string extension
0482         EFileSystem::get_file_name(string $filename) -> returns string name
0483         EFileSystem::rename_file(string $from, string $to) -> rename file
0484         EFileSystem::move_uploaded_file_in($path,$newname) -> move uploaded file to $path and change name to $newname
0485         EFileSystem::get_uploaded_file_name() -> returns original uploaded file name.
0486         
0487 ------------------------------------------------------------------------------
0488 |                       15 . Network Socket object                           |
0489 ------------------------------------------------------------------------------
0490 
0491 GFX permits also to interact with other APIs and websites through ENetworkSocket.
0492 ENetworkSocket is an object that makes http requests with get and post data to the specified address.
0493         
0494         $s = new ENetworkSocket("http://localhost");
0495         $c = $s->get("index.php");
0496         
0497 This will set content of $c to the content of http://localhost/index.php performing a get request.
0498         
0499         $postdata = array(
0500                                                 "name"    => "john",
0501                                                 "surname" => "smith"
0502                                                 )
0503         
0504         $s = new ENetworkSocket("http://localhost");
0505         $c = $s->post("index.php", $postdata);
0506         
0507 This will set content of $c to the content of http://localhost/index.php performing a post request and
0508 sending $postdata as $_POST value.
0509 
0510 ------------------------------------------------------------------------------
0511 |                          16 . XML utilities                                |
0512 ------------------------------------------------------------------------------
0513 
0514 In order to simply read xml GFX3 offers a small class still WIP to handle XML. Small reference:
0515         
0516         EXmlParser::to_array($raw_xml); -> turns raw xml into a structured array.
0517 
0518 ------------------------------------------------------------------------------
0519 |                            17 . More tools                                 |
0520 ------------------------------------------------------------------------------
0521 
0522 If your installation is on / you will have also some utils and debugging tools like
0523 the realtime GFX console in which you can type and execute your code very fast just
0524 to try it, inspect and code faster. You'll find this tool under /utils/index.php
0525 
0526 If you want to know more about the usage that APC has in your system you can also 
0527 go to /utils/apc.php
0528 
0529 ------------------------------------------------------------------------------
0530 |                               18 . Contacts                                |
0531 ------------------------------------------------------------------------------
0532 
0533 This manual has been written by snizzo during an afternoon.
0534 If there are any error please mail me or open a ticket here on github:
0535 
0536         https://github.com/snizzo/gfx-framework
0537         happy.snizzo@gmail.com