function bail($msg) { $html = ''; if (class_exists('Router')) { $router = Router::singleton(); $html .= ' <h1 style="margin-bottom:0;">' . $router->controller . '</h1> <h3 style="margin:4px 0;">Action: ' . $router->action . '</h3> <h3 style="margin:4px 0;">Method: ' . $router->method . '</h3> <h3 style="margin:6px 0 2px 0;"><u>Params</u></h3></dt> ' . array_dump($router->params()) . ' '; } $html .= '<h2 style="margin-bottom:2px;">FATAL ERROR</h2>' . $msg . '<br>'; $trace = debug_backtrace(); $html .= '<h2 style="margin-bottom:6px;">BACKTRACE</h2>'; foreach ($trace as $t) { $html .= ' FILE: ' . $t['file'] . '<br> LINE: ' . $t['line'] . '<br> '; isset($t['class']) ? $function = $t['class'] . '->' . $t['function'] : ($function = $t['function']); $html .= 'FUNCTION: ' . $function . '<br><br>'; } echo $html; trigger_error(strip_tags($msg)); exit; }
/** Main Utilities Helper functions used throughout the system. @package utility */ function bail($msg) { if (is_array($msg)) { $msg = array_dump($msg); } $html = ''; if (class_exists('Router')) { $router = Router::singleton(); $html .= ' <h1 style="margin-bottom:0;">' . $router->controller . '</h1> <h3 style="margin:4px 0;">Action: ' . $router->action . '</h3> <h3 style="margin:4px 0;">Method: ' . $router->method . '</h3> <h3 style="margin:6px 0 2px 0;"><u>Params</u></h3></dt> ' . array_dump($router->params()) . ' '; } $html .= ' <h2 style="margin-bottom:2px;">ERROR MESSAGE</h2><pre>' . $msg . '</pre><br> <h2 style="margin-bottom:6px;">BACKTRACE</h2> ' . backtrace() . ' '; echo $html; trigger_error(strip_tags($msg)); exit; }
function destroyRecords($model, $criteria) { $records = getMany($model, $criteria); $i = 0; if ($records) { foreach ($records as $record) { $record->destroyAssociatedRecords(); $record->delete(); $i++; } echo "Deleted {$i} test {$model}: " . array_dump($criteria) . "<br>"; } else { echo "No test {$model} found: " . array_dump($criteria) . "<br>"; } }
function bail($msg = '') { if (is_array($msg)) { $msg = array_dump($msg); } $html = ''; $html .= ' <h2 style="margin-bottom:2px;">ERROR MESSAGE</h2>' . $msg . '<br> <h2 style="margin-bottom:6px;">BACKTRACE</h2> ' . backtrace() . ' '; echo $html; trigger_error(strip_tags($msg)); exit; }
function find($view_function_name) { if (isset($this->custom_locations[$view_function_name])) { return $this->custom_locations[$view_function_name]; } $probable_filename = snake_case($view_function_name); $reg_ex = '/[^_]*/'; preg_match($reg_ex, $probable_filename, $matches); $folder_name = $matches[0]; $default_path = 'view' . DIRECTORY_SEPARATOR . $folder_name . DIRECTORY_SEPARATOR . $probable_filename . '.php'; if (file_exists($default_path)) { return $default_path; } $i = 0; $test_filename = array(); foreach ($this->_view_paths as $test_path) { $i++; $test_filename[$i] = $test_path . DIRECTORY_SEPARATOR . $probable_filename . '.php'; if (file_exists($test_filename[$i])) { return $test_filename[$i]; } } bail("View {$view_function_name} could not be found.<br>GTD looked in the following locations:<br>{$default_path}<br>" . array_dump($test_filename)); }
function array_dump($a) { $s .= "Array {\n"; foreach ($a as $k => $e) { $s .= "[{$k}] => "; if (is_array($e)) { $s .= array_dump($e); } else { $s .= $e; } $s .= "\n"; } $s .= "}\n"; return $s; }
function WriteLog($message, $flag) { global $config; $enabled = $config['logs']['enabled']; if (!$enabled) { return false; } $dir = $config['logs']['dir']; date_default_timezone_set($config['logs']['timezone']); $varstr = array_dump($_GET); $type = NULL; $ip = $_SERVER['REMOTE_ADDR']; $hostname = gethostbyaddr($ip); $executed = $_SERVER['PHP_SELF']; $timestamp = date('Y-M-d H:i:s T'); $message = str_replace("\n", '', $message); $message = str_replace("<br>", '', $message); switch ($flag) { case BAYONET_LOG_HACK: if (!file_exists($dir . 'hacks.log')) { break; } $fp = fopen($dir . 'hacks.log', 'a'); $type = 'HACK'; $full_message = "TIMESTAMP: {$timestamp}\n\t\tIP: {$ip}\n\t\tHOSTNAME: {$hostname}\n\t\tACTION: {$type} of {$executed}\n\t\tDEFERRAL: {$message}\n\t\tVAR: ({$varstr})\n\n"; break; case BAYONET_LOG_ERROR: if (!file_exists($dir . 'error.log')) { break; } $fp = fopen($dir . 'error.log', 'a'); $type = 'ERROR'; $full_message = "({$timestamp}) - {$type} - {$message} - ({$varstr})\n"; break; case BAYONET_LOG_WARN: if (!file_exists($dir . 'warn.log')) { break; } $fp = fopen($dir . 'warn.log', 'a'); $type = 'WARN'; $full_message = "({$timestamp}) - {$type} - {$message} - ({$varstr})\n"; break; case BAYONET_LOG_INFO: if (!file_exists($dir . 'info.log')) { break; } $fp = fopen($dir . 'info.log', 'a'); $type = 'INFO'; $full_message = "({$timestamp}) - {$type} - {$message}\n"; break; default: echo 'To log something, you need to define a log to write to.<br>'; return; } if (file_exists($fp)) { fwrite($fp, $full_message); fclose($fp); } else { //echo "could not write to file because file does not exist.<br />"; } }
/** * Checks the NOAA FTP site for new TWL files for the Sites stored in the database. */ public static final function checkForNewFTPFiles() { if (is_null(self::$databaseCredentials)) { throw new InvalidOperationException('Database credentials must be set at the class level to allow this action to take place.'); } ini_set('max_execution_time', 600); date_default_timezone_set('UTC'); $DBH = call_user_func_array('DatabaseHelper::getInstance', self::$databaseCredentials); // Create a list of contents of the ftp root $ftpConnection = ftp_connect('polar.ncep.noaa.gov'); ftp_login($ftpConnection, 'anonymous', ''); ftp_pasv($ftpConnection, TRUE); if (!ftp_chdir($ftpConnection, 'nwps')) { exit; } $rootDirContentsArray = self::ftpFolderList($ftpConnection); // Loop through the verbose ftp root directory contents list and create a simple 2D array where the 1st // dimension id the name of the site and the 2nd dimension is the dates covered by the site. $ftpAvailableSitesArray = array(); foreach ($rootDirContentsArray as $rootDirContentName => $dateDirContentProperties) { if ($dateDirContentProperties['type'] == 'file') { continue; } if ($periodPosition = strpos($rootDirContentName, '.')) { $folderSite = strtolower(substr($rootDirContentName, 0, $periodPosition)); $folderDate = substr($rootDirContentName, $periodPosition + 1); $pattern = '/^20[12][0-9][01][0-9][0-3][0-9]$/'; if (preg_match($pattern, $folderDate)) { $ftpAvailableSitesArray[$folderSite][] = $folderDate; } else { } } } foreach ($ftpAvailableSitesArray as &$ftpSite) { sort($ftpSite, SORT_NUMERIC); } unset($rootDirContentName, $dateDirContentProperties, $ftpSite); array_dump($ftpAvailableSitesArray); // Recover the details of sites in the database inc. Name and Last Import Date $existingSitesQuery = <<<MYSQL SELECT * FROM sites MYSQL; $existingSitesResult = DatabaseHelper::query($DBH, $existingSitesQuery); // Loop through the sites in the database while ($dbSiteArray = $existingSitesResult->fetch(PDO::FETCH_ASSOC)) { // Look to see if the site in the database has any folders in the ftp root directory list. If not, skip // the site. $site = strtolower($dbSiteArray['site']); if (is_null($ftpAvailableSitesArray[$site])) { continue; } // If there is a previous import for the site set the import from date as 1 hour after the last import. // If there is no import date then set the import from date to 1 hour before the first available ftp // folder; if (isset($dbSiteArray['date_time'])) { $importFromDateTimeArray = getDate(strtotime($dbSiteArray['date_time'] . ' +1 hour')); } else { $importFromDateTimeArray = getDate(strtotime($ftpAvailableSitesArray[$site][0]) . ' -1 hour'); } // Loop through the dates available for each site on the ftp server. If the date is before the import // from date skip the folder as it has already been processed. If it is equal to the last import date or // later investigate further to see if there is data for an import. foreach ($ftpAvailableSitesArray[$site] as $ftpFolderDate) { $siteFolderDateArray = getDate(strtotime($ftpFolderDate)); // Skip the folder if it is before the import from date. if ($siteFolderDateArray['year'] < $importFromDateTimeArray['year'] || $siteFolderDateArray['year'] == $importFromDateTimeArray['year'] && $siteFolderDateArray['yday'] < $importFromDateTimeArray['yday']) { continue; } // Enter the date folder for the site to look for directories indicating th import hour.. if (!ftp_chdir($ftpConnection, $site . '.' . $ftpFolderDate)) { // If the site date folder can't be accessed skip the folder. continue; } // Catalog the contents of the sites date folder. $dateDirContentsArray = self::ftpFolderList($ftpConnection); // Loop through the contents of the dated site folder looking for numerically named directories // signifying the hour of any import file it contains (hours is 2 digit 24 hour); foreach ($dateDirContentsArray as $importFileHour => $dateDirContentProperties) { // Only look for directories with all numeric names between 0 and 23 and where that name // signifies an import time greater than or equal to the import from hour. If criteria is not met // then skip the content. if ($dateDirContentProperties['type'] == 'file' || !ctype_digit($importFileHour) || $importFileHour < 0 || $importFileHour > 23 || $siteFolderDateArray['year'] == $importFromDateTimeArray['year'] && $siteFolderDateArray['yday'] == $importFromDateTimeArray['yday'] && $importFileHour < $importFromDateTimeArray['hours']) { continue; } // Enter the time folder for the date. if (!ftp_chdir($ftpConnection, $importFileHour)) { // If the site date folder can't be accessed skip the folder. continue; } $timeDirContentsArray = self::ftpFolderList($ftpConnection); foreach ($timeDirContentsArray as $timeDirContentName => $timeDirContentProperties) { if ($timeDirContentProperties['type'] == 'file') { continue; } if (!ftp_chdir($ftpConnection, $timeDirContentName)) { continue; } $timeSubDirContentsArray = self::ftpFolderList($ftpConnection); $targetImportFile = $site . '_nwps_20m_' . $timeDirContentName . '_runup.' . $ftpFolderDate . '_' . $importFileHour . '00'; foreach ($timeSubDirContentsArray as $siteTimeSubDirContentName => $siteTimeSubDirContentProperties) { if ($siteTimeSubDirContentProperties['type'] == 'directory' || $siteTimeSubDirContentName != $targetImportFile) { continue; } $tempFile = tmpfile(); if (@ftp_fget($ftpConnection, $tempFile, $targetImportFile, FTP_ASCII)) { rewind($tempFile); self::importTwlFile($site, $ftpFolderDate . 't' . $timeDirContentName . '0000', $tempFile, TRUE); } else { if (!ftp_chdir($ftpConnection, '../../')) { throw new MyException('Could not change the FTP directory to date sub directory level.'); } break 2; } } if (!ftp_chdir($ftpConnection, '../')) { throw new MyException('Could not change the FTP directory up one level.'); } } if (!ftp_chdir($ftpConnection, '../')) { throw new MyException('Could not change the FTP directory up one level.'); } } if (!ftp_chdir($ftpConnection, '../')) { throw new MyException('Could not change the FTP directory up one level.'); } } } }