Source for file utilities.inc.php

Documentation is available at utilities.inc.php

  1. <?php
  2.  
  3.     /***********************************************\
  4.      * N2F Yverdon v0                              *
  5.      * Copyright (c) 2009 Zibings Incorporated     *
  6.      *                                             *
  7.      * You should have received a copy of the      *
  8.      * Microsoft Reciprocal License along with     *
  9.      * this program.  If not, see:                 *
  10.      * <http://opensource.org/licenses/ms-rl.html> *
  11.     \***********************************************/
  12.  
  13.     /*
  14.      * $Id: utilities.inc.php 38 2009-10-17 10:01:27Z amale $
  15.      */
  16.  
  17.     /**
  18.      * Runs main process for N2 Framework Yverdon.
  19.      *
  20.      * @return null 
  21.      */
  22.     function n2f_proc({
  23.         global $n2f;
  24.  
  25.         $n2f->initModule();
  26.  
  27.         return(null);
  28.     }
  29.  
  30.     /**
  31.      * Pulls a string from the system's string set, given the current system language.
  32.      *
  33.      * @param string $key            Key to search strings definition for
  34.      * @param array $replacements        Optional array of arguments to replace in the defined string
  35.      * @return string 
  36.      */
  37.     function S($keyarray $replacements null{
  38.         global $strings$n2f;
  39.  
  40.         if (!isset($strings[$n2f->cfg->sys_lang])) {
  41.             if ($n2f->debug->showLevel(N2F_DEBUG_ERROR)) {
  42.                 $n2f->debug->throwError(N2F_ERROR_NO_LANGUAGE_SETS('N2F_ERROR_NO_LANGUAGE_SET')'system/config.inc.php');
  43.             }
  44.  
  45.             return('N/A');
  46.         }
  47.  
  48.         if (!isset($strings[$n2f->cfg->sys_lang][$key])) {
  49.             if ($n2f->debug->showLevel(N2F_DEBUG_WARN)) {
  50.                 $n2f->debug->throwWarning(N2F_WARN_LANG_KEY_MISSINGS('N2F_WARN_LANG_KEY_MISSING'array($key))'system/includes/strings.inc.php');
  51.             }
  52.  
  53.             if (!isset($strings['en'][$key])) {
  54.                 return('N/A');
  55.             else {
  56.                 $lang 'en';
  57.             }
  58.         else {
  59.             $lang $n2f->cfg->sys_lang;
  60.         }
  61.  
  62.         $string $strings[$lang][$key];
  63.  
  64.         if ($replacements !== null{
  65.             for ($i 1$i <= count($replacements)$i++{
  66.                 $string str_replace("_%{$i}%_"$replacements[$i 1]$string);
  67.             }
  68.         }
  69.  
  70.         return($string);
  71.     }
  72.  
  73.     /**
  74.      * Returns a string based on the callback provided.
  75.      *
  76.      * @param callback $callback    Callback to return as a string
  77.      * @return string 
  78.      */
  79.     function callback_toString($callback{
  80.         $ret '';
  81.  
  82.         if (is_array($callback)) {
  83.             if (is_object($callback[0])) {
  84.                 $ret .= get_class($callback[0]);
  85.             else {
  86.                 $ret .= $callback[0];
  87.             }
  88.  
  89.             $ret .= "::{$callback[1]}";
  90.         else {
  91.             $ret $callback;
  92.         }
  93.  
  94.         return($ret);
  95.     }
  96.  
  97.     /**
  98.      * Creates a one-way hash from the provided string.  Setting $old to true will use the old algorithm for creating the hash.
  99.      *
  100.      * @param string $str    String to hash
  101.      * @param boolean $old    Whether or not to use the old ZSF algorithm
  102.      * @return string 
  103.      */
  104.     function encStr($str$old false{
  105.         global $n2f;
  106.  
  107.         if ($old{
  108.             $ret '';
  109.  
  110.             for ($i 0$i strlen($str)++$i{
  111.                 $ret .= md5(base64_encode($str[$i]));
  112.             }
  113.  
  114.             return(md5(base64_encode($ret)));
  115.         }
  116.  
  117.         $ret '';
  118.  
  119.         if ($n2f instanceof n2f_cls && $n2f->cfg instanceof n2f_cfg{
  120.             $str .= $n2f->cfg->crypt_hash;
  121.         }
  122.  
  123.         for ($i 0$i strlen($str)++$i{
  124.             $ret .= md5(sha1(base64_encode($str[$i])));
  125.         }
  126.  
  127.         return(md5(sha1(base64_encode($ret))));
  128.     }
  129.  
  130.     /**
  131.      * Hand-made print_r() that provides a method list if $showMethods is set to true.
  132.      *
  133.      * @param mixed $mixed            Item to break down and display
  134.      * @param boolean $return        Whether or not to return the output from debugEcho()
  135.      * @param boolean $showMethods    Whether or not to show methods on objects
  136.      * @param mixed $prefix            Internal for handling recursion
  137.      * @return null 
  138.      */
  139.     function debugEcho($mixed$return false$showMethods false$prefix false{
  140.         // if no prefix
  141.         if ($prefix == false{
  142.             // setup dummy prefix
  143.             $prefix '';
  144.         }
  145.  
  146.         // if it's false, we can assume we're at the beginning
  147.         if ($prefix == false{
  148.             // If we're to return this
  149.             if ($return === true{
  150.                 // Start the output buffering
  151.                 ob_start();
  152.             }
  153.  
  154.             // so output a bit of styling
  155.             echo("<div style='text-align: left;'>");
  156.             echo('<pre>');
  157.         else {
  158.             // just add a newline
  159.             echo("\n");
  160.         }
  161.  
  162.         // if it's an array
  163.         if (is_array($mixed)) {
  164.             // display the header
  165.             echo("{$prefix}Array Elements {\n");
  166.  
  167.             // if there aren't any
  168.             if (count($mixed1{
  169.                 // show that we're runnin on emptyyyy
  170.                 echo("{$prefix}  No Elements;\n");
  171.             else {
  172.                 // loop through the elements
  173.                 foreach ($mixed as $key => $ele{
  174.                     // if it's an array or object
  175.                     if (is_array($ele|| is_object($ele)) {
  176.                         // show the type
  177.                         echo("{$prefix}  [{$key}] => (".gettype($ele).") {");
  178.  
  179.                         // loop through it again
  180.                         debugEcho($ele$return$showMethods"{$prefix}    ");
  181.  
  182.                         // and show the end of it
  183.                         echo("\n{$prefix}  };\n");
  184.                     else {
  185.                         // show the element
  186.                         echo("{$prefix}  [{$key}] => (".gettype($ele).") '{$ele}';\n");
  187.                     }
  188.                 }
  189.             }
  190.  
  191.             // and the end-of
  192.             echo("{$prefix}};");
  193.         else if (is_object($mixed)) {
  194.             // say what kind
  195.             echo("{$prefix}".get_class($mixed)." Object:\n\n{$prefix}Public Properties {\n");
  196.  
  197.             // get the properties
  198.             $vars get_object_vars($mixed);
  199.  
  200.             // if there aren't any properties
  201.             if (count($vars1{
  202.                 // show that there aren't any
  203.                 echo("{$prefix}  No Properties;\n");
  204.             else {
  205.                 // loop through the properties
  206.                 foreach ($vars as $prop => $valu{
  207.                     // if it's the special bugger
  208.                     if ($prop == '_loadedMethods'{
  209.                         // throw them into a temp variable
  210.                         $_loadedMethods $valu;
  211.  
  212.                         // and skip this iteration
  213.                         continue;
  214.                     }
  215.  
  216.                     // if it's an array or it's an object
  217.                     if (is_array($valu|| is_object($valu)) {
  218.                         // show the property and value
  219.                         echo("{$prefix}  [{$prop}] => (".gettype($valu).") {");
  220.  
  221.                         // and throw it into another discovery mission
  222.                         debugEcho($valu$return$showMethods"{$prefix}    ");
  223.  
  224.                         // and get back
  225.                         echo("\n{$prefix}  };\n");
  226.                     else {
  227.                         // show the property and value
  228.                         echo("{$prefix}  [{$prop}] => (".gettype($valu).") '{$valu}';\n");
  229.                     }
  230.                 }
  231.             }
  232.  
  233.             // throw in an end-of bracket
  234.             echo("{$prefix}};\n");
  235.  
  236.             // if we're to show the methods
  237.             if ($showMethods{
  238.                 // greet the audience
  239.                 echo("\n{$prefix}Public Methods {\n");
  240.  
  241.                 // grab the methods we can see
  242.                 $methods get_class_methods(get_class($mixed));
  243.  
  244.                 // if we have other methods
  245.                 if (isset($_loadedMethods&& count($_loadedMethods0{
  246.                     // loop through THEM
  247.                     foreach (array_keys($_loadedMethods['methods']as $method{
  248.                         // and add them onto the end of the pile
  249.                         $methods[$method;
  250.                     }
  251.                 }
  252.  
  253.                 // if we have 0 methods
  254.                 if (count($methods1{
  255.                     // say so!
  256.                     echo("{$prefix}  No Methods;\n");
  257.                 else {
  258.                     // loop through them
  259.                     foreach ($methods as $method{
  260.                         // if it's an array
  261.                         if (is_array($method)) {
  262.                             // show the one it'd be called by
  263.                             echo("{$prefix}  {$method[1]};\n");
  264.                         else {
  265.                             // show the method name
  266.                             echo("{$prefix}  {$method};\n");
  267.                         }
  268.                     }
  269.                 }
  270.  
  271.                 // and do our end-of
  272.                 echo("{$prefix}};\n");
  273.             }
  274.  
  275.             // and the OFFICIAL end-of
  276.             echo("\n{$prefix}End of ".get_class($mixed)." Object");
  277.         else {
  278.             // just a catch-all, shouldn't ever use this
  279.             echo("{$prefix}UNKNOWN of type [".gettype($mixed)."]: '{$mixed}';\n");
  280.         }
  281.  
  282.         // if we're at the 'top' level
  283.         if ($prefix == false{
  284.             // print the bye bye
  285.             echo('</pre>');
  286.             echo("</div>");
  287.  
  288.             // If we've been buffering for output
  289.             if ($return === true{
  290.                 // Grab the output
  291.                 $output ob_get_contents();
  292.  
  293.                 // End cleanly
  294.                 ob_end_clean();
  295.             else {
  296.                 // Otherwise, set to null
  297.                 $output null;
  298.             }
  299.         }
  300.  
  301.         if (!isset($output)) {
  302.             $output null;
  303.         }
  304.  
  305.         // return safely
  306.         return($output);
  307.     }
  308.  
  309.     /**
  310.      * Tries to find and replace the search string inside of the subject string once.
  311.      *
  312.      * @param string $search        String to find inside of $subject string
  313.      * @param string $replace    String to replace first instance of $search string
  314.      * @param string $subject    String to perform actions upon
  315.      * @return string 
  316.      */
  317.     function str_replace_once($search$replace$subject{
  318.         if (strlen($search|| strlen($replace|| strlen($searchstrlen($subject)) {
  319.             return($subject);
  320.         }
  321.  
  322.         $offset strpos($subject$search);
  323.  
  324.         if ($offset === false{
  325.             return($subject);
  326.         }
  327.  
  328.         $search_len strlen($search);
  329.         $subject_len strlen($subject);
  330.         $after_start ($offset $search_len);
  331.         $after_len ($subject_len ($offset $search_len));
  332.  
  333.         $before substr($subject0$offset);
  334.         $after substr($subject$after_start$after_len);
  335.  
  336.         return($before $replace $after);
  337.     }
  338.  
  339.     /**
  340.      * Finds and replaces text contained within the $start and $end tags with the $replace sequence.  Use of the text between $start and $end is signified in the $replace string using the string '%TEXT%'.
  341.      *
  342.      * @param stringt $start        Beginning tag surrounding text
  343.      * @param string $end        End tag surrounding text
  344.      * @param string $replace    String to replace tags and inner text with (reference inner text with %TEXT%)
  345.      * @param string $text        Subject string to perform search/replace within
  346.      * @return string 
  347.      */
  348.     function str_replace_contained($start$end$replace$text{
  349.         if (strlen($text<= (strlen($startstrlen($end))) {
  350.             return($text);
  351.         }
  352.  
  353.         $s array(
  354.             'spos'    => strpos($text$start),
  355.             'epos'    => null,
  356.             'len'    => strlen($start),
  357.             'text'    => null
  358.         );
  359.  
  360.         $e array(
  361.             'spos'    => strpos($text$end),
  362.             'epos'    => null,
  363.             'len'    => strlen($end),
  364.             'text'    => null
  365.         );
  366.  
  367.         if ($s['spos'=== false || $e['spos'=== false{
  368.             return($text);
  369.         }
  370.  
  371.         $s['epos'$s['spos'$s['len'];
  372.         $e['epos'$e['epos'$e['len'];
  373.  
  374.         if ($s['epos'== ($e['spos'1)) {
  375.             return($text);
  376.         }
  377.  
  378.         $inner_text substr($text$s['epos']($e['spos'$s['epos']));
  379.         $replace str_replace('%TEXT%'$inner_text$replace);
  380.         $text str_replace($start $inner_text $end$replace$text);
  381.  
  382.         return($text);
  383.     }
  384.  
  385. ?>

Documentation generated on Sat, 23 Jan 2010 11:13:51 -0500 by phpDocumentor 1.4.1