includes/functions.php
changeset 372 5bd429428101
parent 371 dc6026376919
child 373 5c6e09bc7d69
equal deleted inserted replaced
371:dc6026376919 372:5bd429428101
  1290  */
  1290  */
  1291 
  1291 
  1292 function enano_debug_print_backtrace($return = false)
  1292 function enano_debug_print_backtrace($return = false)
  1293 {
  1293 {
  1294   ob_start();
  1294   ob_start();
  1295   echo '<pre>';
  1295   if ( !$return )
       
  1296     echo '<pre>';
  1296   if ( function_exists('debug_print_backtrace') )
  1297   if ( function_exists('debug_print_backtrace') )
  1297   {
  1298   {
  1298     debug_print_backtrace();
  1299     debug_print_backtrace();
  1299   }
  1300   }
  1300   else
  1301   else
  1301   {
  1302   {
  1302     echo '<b>Warning:</b> No debug_print_backtrace() support!';
  1303     echo '<b>Warning:</b> No debug_print_backtrace() support!';
  1303   }
  1304   }
  1304   echo '</pre>';
  1305   if ( !$return )
       
  1306     echo '</pre>';
  1305   $c = ob_get_contents();
  1307   $c = ob_get_contents();
  1306   ob_end_clean();
  1308   ob_end_clean();
  1307   if($return) return $c;
  1309   if($return) return $c;
  1308   else echo $c;
  1310   else echo $c;
  1309   return null;
  1311   return null;
  3242   
  3244   
  3243   $q = $db->sql_query('INSERT INTO ' . table_prefix . 'language(lang_code, lang_name_default, lang_name_native) 
  3245   $q = $db->sql_query('INSERT INTO ' . table_prefix . 'language(lang_code, lang_name_default, lang_name_native) 
  3244                          VALUES(
  3246                          VALUES(
  3245                            \'' . $db->escape($lang_code) . '\',
  3247                            \'' . $db->escape($lang_code) . '\',
  3246                            \'' . $db->escape($lang_name_neutral) . '\',
  3248                            \'' . $db->escape($lang_name_neutral) . '\',
  3247                            \'' . $db->escape($lang_name_native) . '\'
  3249                            \'' . $db->escape($lang_name_local) . '\'
  3248                          );');
  3250                          );');
  3249   if ( !$q )
  3251   if ( !$q )
  3250     $db->_die('functions.php - installing language');
  3252     $db->_die('functions.php - installing language');
  3251   
  3253   
  3252   if ( ENANO_DBLAYER == 'PGSQL' )
  3254   if ( ENANO_DBLAYER == 'PGSQL' )
  3275   {
  3277   {
  3276     echo '<b>Notice:</b> Can\'t load language file, so the specified language wasn\'t fully installed.<br />';
  3278     echo '<b>Notice:</b> Can\'t load language file, so the specified language wasn\'t fully installed.<br />';
  3277     return false;
  3279     return false;
  3278   }
  3280   }
  3279   return true;
  3281   return true;
       
  3282 }
       
  3283 
       
  3284 /**
       
  3285  * Lists available languages.
       
  3286  * @return array Multi-depth. Associative, with children associative containing keys name, name_eng, and dir.
       
  3287  */
       
  3288 
       
  3289 function list_available_languages()
       
  3290 {
       
  3291   // Pulled from install/includes/common.php
       
  3292   
       
  3293   // Build a list of available languages
       
  3294   $dir = @opendir( ENANO_ROOT . '/language' );
       
  3295   if ( !$dir )
       
  3296     die('CRITICAL: could not open language directory');
       
  3297   
       
  3298   $languages = array();
       
  3299   
       
  3300   while ( $dh = @readdir($dir) )
       
  3301   {
       
  3302     if ( $dh == '.' || $dh == '..' )
       
  3303       continue;
       
  3304     if ( file_exists( ENANO_ROOT . "/language/$dh/meta.json" ) )
       
  3305     {
       
  3306       // Found a language directory, determine metadata
       
  3307       $meta = @file_get_contents( ENANO_ROOT . "/language/$dh/meta.json" );
       
  3308       if ( empty($meta) )
       
  3309         // Could not read metadata file, continue silently
       
  3310         continue;
       
  3311         
       
  3312       // Do some syntax correction on the metadata
       
  3313       $meta = enano_clean_json($meta);
       
  3314         
       
  3315       $meta = enano_json_decode($meta);
       
  3316       if ( isset($meta['lang_name_english']) && isset($meta['lang_name_native']) && isset($meta['lang_code']) )
       
  3317       {
       
  3318         $languages[$meta['lang_code']] = array(
       
  3319             'name' => $meta['lang_name_native'],
       
  3320             'name_eng' => $meta['lang_name_english'],
       
  3321             'dir' => $dh
       
  3322           );
       
  3323       }
       
  3324     }
       
  3325   }
       
  3326   
       
  3327   return $languages;
  3280 }
  3328 }
  3281 
  3329 
  3282 /**
  3330 /**
  3283  * Scales an image to the specified width and height, and writes the output to the specified
  3331  * Scales an image to the specified width and height, and writes the output to the specified
  3284  * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
  3332  * file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will
  3891   */
  3939   */
  3892   
  3940   
  3893   return Zend_Json::decode($data, Zend_Json::TYPE_ARRAY);
  3941   return Zend_Json::decode($data, Zend_Json::TYPE_ARRAY);
  3894 }
  3942 }
  3895 
  3943 
       
  3944 /**
       
  3945  * Cleans a snippet of JSON for closer standards compliance (shuts up the picky Zend parser)
       
  3946  * @param string Dirty JSON
       
  3947  * @return string Clean JSON
       
  3948  */
       
  3949 
       
  3950 function enano_clean_json($json)
       
  3951 {
       
  3952   // eliminate comments
       
  3953   $json = preg_replace(array(
       
  3954           // eliminate single line comments in '// ...' form
       
  3955           '#^\s*//(.+)$#m',
       
  3956           // eliminate multi-line comments in '/* ... */' form, at start of string
       
  3957           '#^\s*/\*(.+)\*/#Us',
       
  3958           // eliminate multi-line comments in '/* ... */' form, at end of string
       
  3959           '#/\*(.+)\*/\s*$#Us'
       
  3960         ), '', $json);
       
  3961     
       
  3962   $json = preg_replace('/([,\{\[])([\s]*?)([a-z0-9_]+)([\s]*?):/', '\\1\\2"\\3" :', $json);
       
  3963   
       
  3964   return $json;
       
  3965 }
       
  3966 
       
  3967 /**
       
  3968  * Starts the profiler.
       
  3969  */
       
  3970 
       
  3971 function profiler_start()
       
  3972 {
       
  3973   global $_profiler;
       
  3974   $_profiler = array();
       
  3975   
       
  3976   if ( !defined('ENANO_DEBUG') )
       
  3977     return false;
       
  3978   
       
  3979   $_profiler[] = array(
       
  3980       'point' => 'Profiling started',
       
  3981       'time' => microtime_float(),
       
  3982       'backtrace' => false
       
  3983     );
       
  3984 }
       
  3985 
       
  3986 /**
       
  3987  * Logs something in the profiler.
       
  3988  * @param string Point name or message
       
  3989  * @param bool Optional. If true (default), a backtrace will be generated and added to the profiler data. False disables this, often for security reasons.
       
  3990  */
       
  3991 
       
  3992 function profiler_log($point, $allow_backtrace = true)
       
  3993 {
       
  3994   if ( !defined('ENANO_DEBUG') )
       
  3995     return false;
       
  3996   
       
  3997   global $_profiler;
       
  3998   $backtrace = false;
       
  3999   if ( $allow_backtrace && function_exists('debug_print_backtrace') )
       
  4000   {
       
  4001     list(, $backtrace) = explode("\n", enano_debug_print_backtrace(true));
       
  4002   }
       
  4003   $_profiler[] = array(
       
  4004       'point' => $point,
       
  4005       'time' => microtime_float(),
       
  4006       'backtrace' => $backtrace
       
  4007     );
       
  4008 }
       
  4009 
       
  4010 /**
       
  4011  * Returns the profiler's data (so far).
       
  4012  * @return array
       
  4013  */
       
  4014 
       
  4015 function profiler_dump()
       
  4016 {
       
  4017   return $GLOBALS['_profiler'];
       
  4018 }
       
  4019 
       
  4020 /**
       
  4021  * Generates an HTML version of the performance profile. Not localized because only used as a debugging tool.
       
  4022  * @return string
       
  4023  */
       
  4024 
       
  4025 function profiler_make_html()
       
  4026 {
       
  4027   if ( !defined('ENANO_DEBUG') )
       
  4028     return '';
       
  4029     
       
  4030   $profile = profiler_dump();
       
  4031   
       
  4032   $html = '<div class="tblholder">';
       
  4033   $html .= '<table border="0" cellspacing="1" cellpadding="4">';
       
  4034   
       
  4035   $time_start = $time_last = $profile[0]['time'];
       
  4036   
       
  4037   foreach ( $profile as $i => $entry )
       
  4038   {
       
  4039     $html .= "<tr><th colspan=\"2\">Event $i</th></tr>";
       
  4040     
       
  4041     $html .= '<tr>';
       
  4042     $html .= '<td class="row2">Event:</td>';
       
  4043     $html .= '<td class="row1">' . htmlspecialchars($entry['point']) . '</td>';
       
  4044     $html .= '</tr>';
       
  4045     
       
  4046     $time = $entry['time'] - $time_start;
       
  4047     
       
  4048     $html .= '<tr>';
       
  4049     $html .= '<td class="row2">Time since start:</td>';
       
  4050     $html .= '<td class="row1">' . $time . 's</td>';
       
  4051     $html .= '</tr>';
       
  4052     
       
  4053     $time = $entry['time'] - $time_last;
       
  4054     
       
  4055     $html .= '<tr>';
       
  4056     $html .= '<td class="row2">Time since last event:</td>';
       
  4057     $html .= '<td class="row1">' . $time . 's</td>';
       
  4058     $html .= '</tr>';
       
  4059     
       
  4060     if ( $entry['backtrace'] )
       
  4061     {
       
  4062       $html .= '<tr>';
       
  4063       $html .= '<td class="row2">Called from:</td>';
       
  4064       $html .= '<td class="row1">' . htmlspecialchars($entry['backtrace']) . '</td>';
       
  4065       $html .= '</tr>';
       
  4066     }
       
  4067     
       
  4068     $time_last = $entry['time'];
       
  4069   }
       
  4070   $html .= '</table></div>';
       
  4071   
       
  4072   return $html;
       
  4073 }
       
  4074 
       
  4075 // Might as well start the profiler, it has no external dependencies except from this file.
       
  4076 profiler_start();
       
  4077 
  3896 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  4078 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  3897 
  4079 
  3898 ?>
  4080 ?>