PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/EngineHelpers.php

https://github.com/poitch/dokin
PHP | 114 lines | 58 code | 12 blank | 44 comment | 9 complexity | 78fba99a244ab59cafb3513094307055 MD5 | raw file
  1<?php
  2/**
  3 *****************************************************************************
  4 ** Copyright (c) 2007-2010 Jerome Poichet <jerome@frencaze.com>
  5 **
  6 ** This software is supplied to you by Jerome Poichet in consideration of 
  7 ** your agreement to the following terms, and your use, installation, 
  8 ** modification or redistribution of this software constitutes acceptance of 
  9 ** these terms. If you do not agree with these terms, please do not use, 
 10 ** install, modify or redistribute this software.
 11 **
 12 ** In consideration of your agreement to abide by the following terms, and 
 13 ** subject to these terms, Jerome Poichet grants you a personal, non-exclusive
 14 ** license, to use, reproduce, modify and redistribute the software, with or 
 15 ** without modifications, in source and/or binary forms; provided that if you
 16 ** redistribute the software in its entirety and without modifications, you 
 17 ** must retain this notice and the following text and disclaimers in all such 
 18 ** redistributions of the software, and that in all cases attribution of 
 19 ** Jerome Poichet as the original author of the source code shall be included
 20 ** in all such resulting software products or distributions.
 21 **
 22 ** Neither the name, trademarks, service marks or logos of Jerome Poichet may
 23 ** be used to endorse or promote products derived from the software without 
 24 ** specific prior written permission from Jerome Poichet. Except as expressly
 25 ** stated in this notice, no other rights or licenses, express or implied, are
 26 ** granted by Jerome Poichet herein, including but not limited to any patent
 27 ** rights that may be infringed by your derivative works or by other works in
 28 ** which the software may be incorporated.
 29 ** 
 30 ** The software is provided by Jerome Poichet on an "AS IS" basis. 
 31 ** JEROME POICHET MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT 
 32 ** LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND 
 33 ** FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR ITS USE AND 
 34 ** OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 35 ** 
 36 ** IN NO EVENT SHALL JEROME POICHET BE LIABLE FOR ANY SPECIAL, INDIRECT, 
 37 ** INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 38 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 39 ** OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 40 ** MODIFICATION AND/OR DISTRIBUTION OF THE SOFTWARE, HOWEVER CAUSED AND 
 41 ** WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT 
 42 ** LIABILITY OR OTHERWISE, EVEN IF JEROME POICHET HAS BEEN ADVISED OF THE 
 43 ** POSSIBILITY OF SUCH DAMAGE.
 44 *****************************************************************************
 45 **/
 46require_once DOKIN_DIR.'Controller.php';
 47require_once DOKIN_DIR.'Config.php';
 48
 49function template($path, $hData = array())
 50{
 51    global $sEngineDebug,$sControllerDebug;
 52
 53    $sTemplatePath = 'app/templates/'.$path;
 54
 55    if (!file_exists($sTemplatePath)) {
 56        throw new Exception('template '.$path.' not found');
 57    }
 58
 59    $oController = Controller::get_instance();
 60    if ($oController) {
 61        extract($oController->get());
 62    }
 63    if (is_array($hData) && sizeof($hData)) {
 64        extract($hData);
 65    }
 66    include($sTemplatePath);
 67}
 68
 69function include_content()
 70{
 71    global $_content_template;
 72    template($_content_template);
 73}
 74
 75function render_partial($sTemplate,$hData)
 76{
 77    if (is_array($hData) && sizeof($hData)) {
 78        extract($hData);
 79    }
 80
 81    $sTemplatePath = 'app/templates/'.$sTemplate;
 82
 83    if (!file_exists($sTemplatePath)) {
 84        throw new Exception('template '.$path.' not found');
 85    }
 86
 87    ob_start();
 88    include($sTemplatePath);
 89    $sContent = ob_get_contents();
 90    ob_end_clean();
 91    return $sContent;
 92}
 93
 94function set_cookie($key, $value, $life = NULL)
 95{
 96    $aServerAddress = explode('.', $_SERVER['SERVER_NAME']);
 97    $sDomain = '.'.$aServerAddress[count($aServerAddress)-2].'.'.$aServerAddress[count($aServerAddress)-1];
 98    if ($life !== NULL) {
 99        $time = time() + $life;
100    } else {
101        $time = NULL;
102    }
103    setcookie($key, $value, $time, '/', $sDomain);
104}
105
106function uuid() 
107{
108    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
109            mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
110            mt_rand( 0, 0x0fff ) | 0x4000,
111            mt_rand( 0, 0x3fff ) | 0x8000,
112            mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
113}
114