Advertisement
Guest User

Untitled

a guest
Jul 9th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class RFUtil {
  5.     protected static $url;
  6.     public static function RequestRedirect($url) {
  7.         self::$url = $url;
  8.     }
  9.  
  10.     public static function emitJson ($object, $extraHeaders = array()) {
  11.         if(headers_sent()) {
  12.             die("Error! Please do not send any data directly to the browser. For logging, use Dashboard::log instead");
  13.         }
  14.  
  15.         if(PHP_SAPI !== "cli")
  16.         {
  17.             // Set the content-type header.
  18.             foreach($extraHeaders as $header)
  19.             {
  20.                 header($header);
  21.             }
  22.  
  23.             // VERY AGGRESSIVELY PREVENT CACHING!!!
  24.             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  25.             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  26.             header("Cache-Control: no-store, no-cache, must-revalidate");
  27.             header("Cache-Control: post-check=0, pre-check=0", false);
  28.             header("Pragma: no-cache");
  29.  
  30.             // Ensure it's parsed as JSON
  31.             header('Content-Type: application/json;');
  32.         }
  33.  
  34.         if(RFConfig::isSetAndTrue('debug'))
  35.         {
  36.             $object['logs'] = RFLog::__getMessages ();
  37.             $object['rfdt'] = array(
  38.                     'queries' => RFDevTools::getQueries (),
  39.                     'dataSources' => RFDevTools::getDataSources(),
  40.                     'diagnostics' => RFDevTools::getDiagnostics()
  41.                 );
  42.         }
  43.  
  44.         if(isset(self::$url))
  45.         {
  46.             $object['redirect'] = self::$url;
  47.         }
  48.  
  49.  
  50.         $GLOBALS['rfDisableErrors'] = true;
  51.         echo json_encode($object);
  52.         exit();
  53.     }
  54.  
  55.  
  56.     /**
  57.      * Throw an exception into JSON.
  58.      *
  59.      * @param Exception $ex
  60.      */
  61.     public static function Exception (Exception $ex) {
  62.         RFLog::log("[ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR]");
  63.         RFLog::log("Exception: ". $ex->getMessage());
  64.         $trace = $ex->getTrace();
  65.         if(isset($trace[0]['file']) && isset($trace[0]['line']))
  66.             RFLog::log("Trace: ". $trace[0]['file'].":".$trace[0]['line']);
  67.         RFLog::log("[ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR]");
  68.  
  69.         $traceMsg = "";
  70.         foreach($ex->getTrace() as $item)
  71.         {
  72.             if(isset($item['file']) && $item['line'])
  73.                 $traceMsg .= $item['file'].":".$item['line']."\n";
  74.         }
  75.         $object = array(
  76.             'message' => $ex->getMessage(),
  77.             'trace' => $traceMsg,
  78.             'error' => 'RazorflowException'
  79.         );
  80.  
  81.         $extraHeaders = array(
  82.             'HTTP/1.1 500 Internal Exception;',
  83.             'X-RazorFlow-Info: Exception'
  84.         );
  85.  
  86.         self::emitJson($object, $extraHeaders);
  87.     }
  88.  
  89.     public static function Error ($message, $trace = "") {
  90.         RFLog::log("[ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR]");
  91.         RFLog::log("Message: ", $message);
  92.         RFLog::log("Trace: ", $trace);
  93.         RFLog::log("[ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR][ERROR]");
  94.         $object = array(
  95.             'message' => $message,
  96.             'trace' => $trace,
  97.             'error' => 'RazorflowError'
  98.         );
  99.  
  100.         $extraHeaders = array(
  101.             'HTTP/1.1 500 Internal Exception;',
  102.             'X-RazorFlow-Info: Error'
  103.         );
  104.  
  105.         if(!RFRequest::isHTMLRequest())
  106.         {
  107.             self::emitJson($object, $extraHeaders);
  108.         }
  109.  
  110.     }
  111.  
  112.     public static function getSampleDataSource ()
  113.     {
  114.         if(!isset(self::$sampleDs))
  115.         {
  116.             self::$sampleDs = new SQLiteDataSource(RF_FOLDER_ROOT."/demos/databases/chinook.sqlite");
  117.  
  118.             self::$sampleDs->setSQLSource("InvoiceLine JOIN Invoice ON Invoice.InvoiceId = InvoiceLine.InvoiceId JOIN Track ON Track.TrackId = InvoiceLine.TrackId JOIN Album ON Track.AlbumId = Album.AlbumId JOIN Artist ON Album.ArtistId = Artist.ArtistId JOIN Genre ON Track.GenreId = Genre.GenreId");
  119.         }
  120.  
  121.         return self::$sampleDs;
  122.     }
  123.  
  124.     public static function getTempDir () {
  125.         // Get the temporary directory.
  126.         return sys_get_temp_dir();
  127.     }
  128.  
  129.     public static function isCLI() {
  130.         return PHP_SAPI === "cli";
  131.     }
  132.  
  133.     protected static $sampleDs;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement