/** * create our instance and setup class wide variables * * @return void */ function __construct() { //get our debug callback and store it incase of debugging $this->debug_backtrace = Debug::backtrace(); //keep just the second element sa that tells us where the calling class is $this->debug_backtrace = $this->debug_backtrace[1]; //get our config $config = Config::settings('email'); $this->header = $config['header']; $this->footer = $config['footer']; //init our email instance $this->email = new PHPMailer(true); //check if we want to use smtp and set all our required values from the config if ($config['smtp'] === true) { // telling the class to use SMTP $this->email->IsSMTP(); // set the host address $this->email->Host = $config['smtp-host']; // enable SMTP authentication $this->email->SMTPAuth = true; // SMTP connection will not close after each email sent $this->email->SMTPKeepAlive = true; // set the SMTP port for the GMAIL server $this->email->Port = $config['smtp-port']; // SMTP account username $this->email->Username = $config['smtp-username']; // SMTP account password $this->email->Password = $config['smtp-password']; } //add the default from address $this->addFrom($config['from-address'], $config['fromt-name']); }
/** * check the session exists * * @return bool */ static function check() { //get our encrypted session data $check = Input::session('member'); //if the check wasnt found, it returns '-5' (no idea why) //check for a positive int and return if ((int) $check > 0) { \Meagr\Debug::init('log')->add(array('message' => 'Auth check ok', 'class' => __METHOD__, 'status' => 'success', 'backtrace' => Debug::backtrace())); return (int) $check; //otherwise the user isnt logged in } else { \Meagr\Debug::init('log')->add(array('message' => 'Auth check failed', 'class' => __METHOD__, 'status' => 'error', 'backtrace' => Debug::backtrace())); return false; } }
public static function load($classname) { //we need debug, so include it if (!class_exists('Debug')) { require_once CORE_PATH . '/debug.php'; } $classname = ltrim($classname, '\\'); $filename = ''; $namespace = ''; if ($lastNsPos = strrpos($classname, '\\')) { $namespace = substr($classname, 0, $lastNsPos); $classname = substr($classname, $lastNsPos + 1); $filename = SITE_PATH . '/' . strtolower(str_replace('\\', DS, $namespace) . DS); } $filename .= strtolower($classname) . '.php'; if (file_exists($filename) and !class_exists(basename($classname))) { Debug::init('file')->add(array('message' => str_replace(SITE_PATH, '', $filename), 'filesize' => filesize($filename), 'class' => __METHOD__, 'status' => 'success', 'backtrace' => Debug::backtrace())); require_once $filename; return; } Debug::init('file')->add(array('message' => 'Failed to load ' . $filename, 'class' => __METHOD__, 'status' => 'error', 'backtrace' => Debug::backtrace())); return $filename !== false; }
static function emailError($emailAddress, $errno, $errstr, $errfile, $errline, $errcontext, $errorType = "Error") { if (strtolower($errorType) == 'warning') { $colour = "orange"; } else { $colour = "red"; } $data = "<div style=\"border: 5px {$colour} solid\">\n"; $data .= "<p style=\"color: white; background-color: {$colour}; margin: 0\">{$errorType}: {$errstr}<br /> At line {$errline} in {$errfile}\n<br />\n<br />\n</p>\n"; $data .= Debug::backtrace(true); $data .= "</div>\n"; // override smtp-server if needed if (self::$custom_smtp_server) { ini_set("SMTP", self::$custom_smtp_server); } $relfile = Director::makeRelative($errfile); if ($relfile[0] == '/') { $relfile = substr($relfile, 1); } mail($emailAddress, "{$errorType} at {$relfile} line {$errline} (http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']})", $data, "Content-type: text/html\nFrom: errors@silverstripe.com"); }
/** * Logging debug information * * @param int $type * @param string $sql * @param array $bind * @param StatementInterface $result * @return $this */ protected function _debugStat($type, $sql, $bind = array(), $result = null) { if (!$this->_debug) { return $this; } $code = '## ' . getmypid() . ' ## '; $nl = "\n"; $time = sprintf('%.4f', microtime(true) - $this->_debugTimer); if (!$this->_logAllQueries && $time < $this->_logQueryTime) { return $this; } switch ($type) { case self::DEBUG_CONNECT: $code .= 'CONNECT' . $nl; break; case self::DEBUG_TRANSACTION: $code .= 'TRANSACTION ' . $sql . $nl; break; case self::DEBUG_QUERY: $code .= 'QUERY' . $nl; $code .= 'SQL: ' . $sql . $nl; if ($bind) { $code .= 'BIND: ' . var_export($bind, true) . $nl; } if ($result instanceof StatementInterface) { $code .= 'AFF: ' . $result->rowCount() . $nl; } break; } $code .= 'TIME: ' . $time . $nl; if ($this->_logCallStack) { $code .= 'TRACE: ' . Debug::backtrace(true, false) . $nl; } $code .= $nl; $this->_debugWriteToFile($code); return $this; }
/** * Write a backtrace */ function writeTrace() { echo '<h3>Trace</h3>'; Debug::backtrace(); echo '</div>'; }
/** * @brief Print a debug backtrace * * @param integer $trim Number of items to trim from the top of the stack * @param array $stack The stack, if null will get current stack * @param boolean $return If true the result will be returned instead of outputted */ static function backtrace($trim = 1, $stack = null, $return = false) { return Debug::backtrace($trim + 1, $stack, $return); if (!$stack) { $stack = debug_backtrace(false); } $trace = array(); foreach ($stack as $i => $method) { $args = array(); if ($i > $trim - 1) { if (isset($method['args'])) { foreach ($method['args'] as $arg) { $args[] = gettype($arg); } } $mark = $i == $trim ? 'in' : ' invoked from'; if (!isset($method['file'])) { if (isset($method['type'])) { $trace[] = sprintf(" %s %s%s%s(%s) - %s:%d", $mark, $method['class'], $method['type'], $method['function'], join(',', $args), '???', 0); } else { $trace[] = sprintf(" %s %s(%s) - %s:%d", $mark, $method['function'], join(',', $args), '???', 0); } } else { if (isset($method['type'])) { $trace[] = sprintf(" %s %s%s%s(%s) - %s:%d", $mark, $method['class'], $method['type'], $method['function'], join(',', $args), str_replace(SYS_PATH, '', $method['file']), $method['line']); } else { $trace[] = sprintf(" %s %s(%s) - %s:%d", $mark, $method['function'], join(',', $args), str_replace(SYS_PATH, '', $method['file']), $method['line']); } } } } if ($return) { return join("\n", $trace) . "\n"; } Console::debugEx(LOG_WARN, 'Backtrace', "%s", join("\n", $trace) . "\n"); if (LOGFILE) { fprintf(LOGFILE, join("\n", $trace) . "\n"); } }
/** * Write a backtrace */ function writeTrace() { Debug::backtrace(); }
/** * accepts $viewname as a php file name, * or alternatively module::filename could be used to specify the target app * * @param viewname string The name of the view file * @param data array Extra data that is to be passed to the view * @param template string The template file to be used to render the required view * * @return html string */ static function view($viewname, $data = null, $template = 'default') { //check for the default mvc location $view = MODULE_PATH . '/views/' . $viewname . '.php'; if (!is_file($view)) { //get the class that the request is coming from $calling_class = get_called_class(); //get the class name and swap \ for / as well as controllers for views to check HMVC $class = strtolower(str_replace('controllers', 'views', str_replace('\\', '/', $calling_class))); $view = SITE_PATH . '/' . $class . '/' . $viewname . '.php'; } //specify a particular app to use by searching for :: in the viewname // so member::viewname would look in ... app/member/views/viewname if (!is_file($view) and strpos($viewname, '::')) { $segments = explode('::', $viewname); $class = $segments[0]; $viewname = $segments[1]; $view = MODULE_PATH . '/' . strtolower($class) . '/views/' . strtolower($viewname) . '.php'; } if (is_file($view)) { if (!empty($data) and is_array($data)) { extract($data); } ob_start(); require $view; $content = ob_get_contents(); ob_end_clean(); Debug::init('log')->add(array('message' => 'Including view: ' . $view, 'class' => __METHOD__, 'status' => 'success', 'backtrace' => Debug::backtrace())); //render the template file with the compiled view data self::template($template, $content); //if the view file cannot be found } else { throw new MeagrException('The requested view file "' . $view . '" could not be found'); } }
/** * Set the environment type of the current site. * * Typically, a SilverStripe site have a number of environments: * - development environments, such a copy on your local machine. * - test sites, such as the one you show the client before going live. * - the live site itself. * * The behaviour of these environments often varies slightly. For example, development sites may have errors dumped to the screen, * and order confirmation emails might be sent to the developer instead of the client. * * To help with this, Sapphire support the notion of an environment type. The environment type can be dev, test, or live. * * You can set it explicitly with Director::set_environment_tpye(). Or you can use {@link Director::set_dev_servers()} and {@link Director::set_test_servers()} * to set it implicitly, based on the value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, then * the environment type will be test or dev. Otherwise, the environment type will be live. * * Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and then push the site into dev * mode for the remainder of the session. Putting ?isDev=0 onto the URL can turn it back. * * Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and then push the site into test * mode for the remainder of the session. Putting ?isTest=0 onto the URL can turn it back. * * Generally speaking, these methods will be called from your _config.php file. * * Once the environment type is set, it can be checked with {@link Director::isDev()}, {@link Director::isTest()}, and * {@link Director::isLive()}. * * @param $et string The environment type: dev, test, or live. */ static function set_environment_type($et) { if ($et != 'dev' && $et != 'test' && $et != 'live') { Debug::backtrace(); user_error("Director::set_environment_type passed '{$et}'. It should be passed dev, test, or live", E_USER_WARNING); } else { self::$environment_type = $et; } }
/** * Shows an error. It will stop script execution if the error code is not * in the errors.continue_on whitelist. * * @param Exception $e the exception to show * @return void */ public static function show_php_error(\Exception $e) { $fatal = (bool) (!in_array($e->getCode(), \Config::get('errors.continue_on', array()))); $data = static::prepare_exception($e, $fatal); if ($fatal) { $data['contents'] = ob_get_contents(); while (ob_get_level() > 0) { ob_end_clean(); } ob_start(\Config::get('ob_callback', null)); } else { static::$non_fatal_cache[] = $data; } if (\Fuel::$is_cli) { \Cli::write(\Cli::color($data['severity'] . ' - ' . $data['message'] . ' in ' . \Fuel::clean_path($data['filepath']) . ' on line ' . $data['error_line'], 'red')); if (\Config::get('cli_backtrace')) { \Cli::write('Stack trace:'); \Cli::write(\Debug::backtrace($e->getTrace())); } return; } if ($fatal) { if (!headers_sent()) { $protocol = \Input::server('SERVER_PROTOCOL') ? \Input::server('SERVER_PROTOCOL') : 'HTTP/1.1'; header($protocol . ' 500 Internal Server Error'); } $data['non_fatal'] = static::$non_fatal_cache; try { exit(\View::forge('errors' . DS . 'php_fatal_error', $data, false)); } catch (\FuelException $view_exception) { exit($data['severity'] . ' - ' . $data['message'] . ' in ' . \Fuel::clean_path($data['filepath']) . ' on line ' . $data['error_line']); } } try { echo \View::forge('errors' . DS . 'php_error', $data, false); } catch (\FuelException $e) { echo $e->getMessage() . '<br />'; } }
/** * The default 404 method * * @return mixed */ public static function GET_404() { View::view('404', array('controller' => __METHOD__, 'backtrace' => Debug::backtrace())); }
/** * set the body of content to be sent to the browser, * buffer all output and store in $this->body * * @return object */ function body() { //if we already have content if (!empty($this->body) or $this->cache_exists) { //return self and move on return $this; } //get our route object $route = $this->router->route; //properly capitalise our namespace $route = Router::namespaceRoutePattern($route); //get our arguments $args = $this->router->arguments ?: array(); //get our class and method list($class, $method) = explode('::', $route->getMappedPattern()); \Meagr\Debug::init('log')->add(array('message' => 'Route: ' . $class . ' ' . $method, 'class' => __METHOD__, 'status' => 'success', 'backtrace' => Debug::backtrace())); //start our buffering ob_start(); //our before function call_user_func_array(array($class, '__before'), $args); //call our function and class call_user_func_array(array($class, $method), $args); //our after function call_user_func_array(array($class, '__after'), $args); //assign the buffer to our body variable $this->body = ob_get_contents(); //finish buffering and clean the output ob_end_clean(); //allow for chaining return $this; }
/** * Dispatch an email notification message when an error is triggered. * Uses the native PHP mail() function. * * @param string $emailAddress * @param string $errno * @param string $errstr * @param string $errfile * @param int $errline * @param string $errcontext * @param string $errorType "warning" or "error" * @return boolean */ static function emailError($emailAddress, $errno, $errstr, $errfile, $errline, $errcontext, $errorType = "Error") { if(strtolower($errorType) == 'warning') { $colour = "orange"; } else { $colour = "red"; } $data = "<div style=\"border: 5px $colour solid\">\n"; $data .= "<p style=\"color: white; background-color: $colour; margin: 0\">$errorType: $errstr<br /> At line $errline in $errfile\n<br />\n<br />\n</p>\n"; $data .= Debug::backtrace(true); $data .= "</div>\n"; // override smtp-server if needed if(self::$custom_smtp_server) ini_set("SMTP", self::$custom_smtp_server); $relfile = Director::makeRelative($errfile); if($relfile[0] == '/') $relfile = substr($relfile,1); return mail($emailAddress, "$errorType at $relfile line $errline (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI])", $data, "Content-type: text/html\nFrom: errors@silverstripe.com"); }