Source for file functions.php

Documentation is available at functions.php

  1. <?php
  2.     /**
  3.      * functions.php
  4.      * 
  5.      * @copyright 2008 Soomp
  6.      * @author Marcio Merlone <mmerlone@gmail.com>
  7.      * @version 1.0
  8.      * @since 11/12/2006
  9.      * @package Soomp
  10.      */
  11.     /* $Id: functions.php,v 1.29 2007/05/10 19:13:01 mmerlone Exp $ */
  12.  
  13.     /**
  14.      * Verifica se passou pelo index.php/ajax.php/soap.php
  15.      */
  16.     if(!defined('INDEX'&& !defined('AJAX'&& !defined('SOAP')){
  17.         die('Este script nao pode ser acessado diretamente!');
  18.     }
  19.  
  20.     /**
  21.      * Substitui variáveis por seus valores em uma string
  22.      * @param mixed $mixVars array ou objeto associativo com os campos e valores
  23.      * @param string $string string a ter variáveis substituídas por seus valores
  24.      * @return string $string string com as variáveis substituídas
  25.      */
  26.     function compileString($mixVars$string){
  27.         if(is_object($mixVars)){
  28.             $mixVars get_object_vars($mixVars);
  29.         }
  30.         if(is_array($mixVars)){
  31.             extract($mixVars);
  32.         }
  33.         $evalString addcslashes($string'"');
  34.         eval("\$string = \"$evalString\";");
  35.         return $string;
  36.     }
  37.  
  38.     /**
  39.      * Converte a string de um charset (geralmente utf-8 do framework)
  40.      * para o charset do projeto conforme config.
  41.      * @see CFG_CHARSET
  42.      * @param string a string a ser convertida
  43.      * @return string a string convertida
  44.      */
  45.     function convertCharset($string){
  46.         $charset mb_detect_encoding($string'UTF-8, ISO-8859-1, US-ASCII, auto');
  47.         return mb_convert_encoding($stringCFG_CHARSET$charset);
  48.     }
  49.  
  50.     /**
  51.      * Faz o var_dump ou print_r de variáveis no browser conforme valor de
  52.      * CFG_DEBUG em config.php:
  53.      * - define('CFG_DEBUG', 0) => Desabilita debug
  54.      * - define('CFG_DEBUG', 1) => Utiliza print_r
  55.      * - define('CFG_DEBUG', 2) => Utiliza o var_dump
  56.      * 
  57.      * O segundo parâmetro define um rótulo para identificação do dump, exibido logo antes
  58.      * O terceiro parâmetro define que variáveis superglobais devem ser debugadas adicionalmente:
  59.      * - 0 = Nenhuma variável adicional
  60.      * - 1 = $_GET
  61.      * - 2 = $_POST
  62.      * - 4 = $_COOKIE
  63.      * - 8 = $_SESSION
  64.      * - 16 = $_SERVER
  65.      * - 32 = $_FILES
  66.      * - 64 = $_ENV
  67.      * Ex.: Para debugar $_POST (2), $_SESSION (8) e $_ENV (64) usa-se $lvl = 74 (64+8+2=74)
  68.      * <code>
  69.      * debug($foo,'foo:',74);
  70.      * </code>
  71.      * 
  72.      * @param mixed $var variável a ser inspecionada.
  73.      * @param string $lbl rótulo para a variável a ser debugada
  74.      * @param integer $lvl Debug level de outras variáveis, soma dos valores:
  75.      * @uses CFG_DEBUG
  76.      * @link http://us2.php.net/manual/en/language.operators.bitwise.php
  77.      */
  78.     function debug($var$lbl '::'$lvl 0){
  79.         if(CFG_DEBUG && ini_get('display_errors')){
  80.             switch(CFG_DEBUG){
  81.                 case 1:
  82.                     $dump 'print_r';
  83.                     break;
  84.  
  85.                 case 2:
  86.                     $dump 'var_dump';
  87.                     break;
  88.  
  89.                 default:
  90.                     $dump 'print_r';
  91.                     break;
  92.  
  93.             
  94.             if(!is_null($var|| !is_null($lbl)){
  95.  
  96.                 $strBacktrace '';
  97.                 $backtrace debug_backtrace();
  98.                 if(basename($backtrace[0]['file']!= 'debug.php'){
  99.                     foreach($backtrace as $call){
  100.                         $function = isset($call['function']$call['function'''
  101.                         $strBacktrace .= basename($call['file']).':'.$call['line'].' ('.$function.')'."\n";
  102.                         /* Caso queira incluir os argumentos da função
  103.                         if(count($call['args'])){
  104.                             $strBacktrace .= 'args: '.dump($call['args']);
  105.                         }
  106.                         */
  107.                     }
  108.                 }else{
  109.                     $backtrace false;
  110.                 }
  111.                 if($backtrace{
  112.                     $str "==================================================\n$strBacktrace\n-------------------------\n";
  113.                 }else{
  114.                     $str '';
  115.                 }
  116.  
  117.                 ob_start();
  118.                 if(!is_null($lbl)) echo $lbl.': ';
  119.                 $dump($var);
  120.                 $str .= ob_get_contents();
  121.  
  122.  
  123.                 if(defined('INDEX')) {
  124.                     $str htmlentities($strENT_QUOTES);
  125.                     $str str_replace('{!''*'$str)// smarty?
  126.                     $str .= "\n-----------------------------";
  127.                 }
  128.                 ob_end_clean();
  129.  
  130.                 outputDivDebug();
  131.                 $ret outputStr(preg_replace('/\r?\n/','\n'$str)."\\n");
  132.                 if(defined('AJAX')){
  133.                     return $ret;
  134.                 }
  135.             }
  136.  
  137.             /**
  138.              * PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO 
  139.              * 
  140.              * Função recursiva! Nunca chamar debug($xxx, $lvl) com ($lvl != 0)
  141.              * aqui dentro da própria função debug!!!!!!
  142.              * 
  143.              * PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO PERIGO 
  144.              */
  145.             if($lvl){
  146.                 if(($lvl 1== 1){
  147.                     debug($_GET,'_GET:',0);
  148.                 }
  149.                 if(($lvl 2== 2){
  150.                     debug($_POST,'_POST:',0);
  151.                 }
  152.                 if(($lvl 4== 4){
  153.                     debug($_COOKIE,'_COOKIE:',0);
  154.                 }
  155.                 if(($lvl 8== 8){
  156.                     debug($_SESSION,'_SESSION:',0);
  157.                 }
  158.                 if(($lvl 16== 16){
  159.                     debug($_SERVER,'_SERVER:',0);
  160.                 }
  161.                 if(($lvl 32== 32){
  162.                     debug($_FILES,'_FILES:',0);
  163.                 }
  164.                 if(($lvl 64== 64){
  165.                     debug($_ENV,'_ENV:',0);
  166.                 }
  167.             // fim if($lvl)
  168.         // fim if(CFG_DEBUG && ini_get('display_errors'))
  169.     }
  170.  
  171.     /**
  172.      * Cria o div de debug no browser.
  173.      * Para depender o mínimo possível do framework, não usa smarty.
  174.      */
  175.     function outputDivDebug(){
  176.         if(defined('INDEX'&& (!isset($_SESSION['debugged']|| !$_SESSION['debugged'])){
  177.                     echo '
  178.             <script type="text/javascript" src="j/drag.js"></script>
  179.             <div id="debug" class="debug" style="left:500px; top:20px;" foo="bar">
  180.                 <div id="move" class="move">
  181.                     <div style="float:left;width:200px;"><span>Debug</span></div>
  182.                     <div style="float:right;width:93px;">
  183.                         <span class="close"><a href="JavaScript:closeDebug();" title="{!tr id="3" value="Fechar"}">X</a></span>
  184.                     </div>
  185.                 </div>
  186.                 <div id="conteudoDebug" style="float: left;">
  187.                 </div>
  188.             </div>
  189.             <script type="text/javascript">
  190.                 function closeDebug(){
  191.                     document.getElementById(\'debug\').style.display="none";
  192.                 }
  193.                 var theHandle = document.getElementById("move");
  194.                 var theRoot   = document.getElementById("debug");
  195.                 Drag.init(theHandle, theRoot);
  196.             </script>';
  197.         }
  198.         $_SESSION['debugged'true;
  199.     }
  200.  
  201.     /**
  202.      * Cria os filhos do div debug com as variáveis dumpeadas
  203.      * @param string $str texto com o dump de uma variável
  204.      */
  205.     function outputStr($str){
  206.         if(defined('INDEX')){
  207.                 echo '<script type="text/javascript">
  208.             var str = "'.$str.'";
  209.             var pStr = document.createElement(\'p\');
  210.             pStr.innerHTML = \'<pre>\'+str+\'<\/pre>\';
  211.             divContent = document.getElementById(\'conteudoDebug\');
  212.             divContent.appendChild(pStr);
  213.             br = document.createElement(\'br\');
  214.             divContent.appendChild(br);
  215.             document.getElementById(\'debug\').style.display = \'block\';
  216.             </script>';
  217.             return ;
  218.         }else{
  219.             return $str;
  220.         }
  221.     }
  222.  
  223.     /**
  224.      * Joga para o debug as tabelas existentes em um banco de dados
  225.      * @param object $db objeto adodb com o banco de dados
  226.      * @param string $id identificação para o debug
  227.      */
  228.     function debugShowTables($db$id 'foo'){
  229.         $foo $db->getAll('SHOW TABLES;');
  230.         debug($foo$id);
  231.     }
  232.  
  233. ?>

Documentation generated on Sun, 09 Mar 2008 23:52:08 -0300 by phpDocumentor 1.4.0

SourceForge.net Logo Support This Project