/** * Save an uploaded file to a new location. If no filename is provided, * the original filename will be used, with a unique prefix added. * * This method should be used after validating the $_FILES array: * * if ($array->check()) * { * // Upload is valid, save it * Upload::save($_FILES['file']); * } * * @param array uploaded file data * @param string new filename * @param string new directory * @param integer chmod mask * @return string on success, full path to new file * @return FALSE on failure */ public static function save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644) { if (!isset($file['tmp_name']) or !is_uploaded_file($file['tmp_name'])) { // Ignore corrupted uploads return FALSE; } if ($filename === NULL) { // Use the default filename, with a timestamp pre-pended $filename = uniqid() . $file['name']; } if (Upload::$remove_spaces === TRUE) { // Remove spaces from the filename $filename = preg_replace('/\\s+/', '_', $filename); } if ($directory === NULL) { // Use the pre-configured upload directory $directory = Upload::$default_directory; } if (!is_dir($directory) or !is_writable(realpath($directory))) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Kohana::debug_path($directory))); } // Make the filename into a complete path $filename = realpath($directory) . DIRECTORY_SEPARATOR . $filename; if (move_uploaded_file($file['tmp_name'], $filename)) { if ($chmod !== FALSE) { // Set permissions on filename chmod($filename, $chmod); } // Return new file path return $filename; } return FALSE; }
public function __construct($directory) { if (!is_dir($directory) or !is_writable($directory)) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Kohana::debug_path($directory))); } // Determine the directory path $this->_directory = realpath($directory) . DIRECTORY_SEPARATOR; }
/** * Creates a new file logger. * * @param string log directory * @param array list of excluded types * @return void */ public function __construct($directory, array $config = NULL) { if (!is_dir($directory) or !is_writable($directory)) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Kohana::debug_path($directory))); } // Determine the directory path $this->_directory = realpath($directory) . DIRECTORY_SEPARATOR; $firephp = FirePHP_Profiler::instance(); if (isset($config)) { $firephp->set_config($config); } $this->_format = $firephp->get_config('log.file.format', 'time --- type: body'); $this->_excluded = $firephp->get_config('log.file.exclude'); }
/** * Download a file to a new location. If no filename is provided, * the original filename will be used, with a unique prefix added. * * @param string remote url * @param string new filename * @param string new directory * @param integer chmod mask * @return array on success, upload style file array * @return false on failure */ public static function download($url, $filename = NULL, $directory = NULL, $chmod = 0644) { if (!Valid::url($url)) { return false; } // If no filename given, use remote filename with uniqid $original_filename = basename(parse_url($url, PHP_URL_PATH)); if ($filename === null) { $filename = uniqid() . $original_filename; } // Remove spaces from the filename if (Upload::$remove_spaces === true) { $filename = preg_replace('/\\s+/', '_', $filename); } // Use the pre-configured upload directory if not given if ($directory === null) { $directory = Upload::$default_directory; } if (!is_dir($directory) || !is_writable(realpath($directory))) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Kohana::debug_path($directory))); } // Make the filename into a complete path $filename = realpath($directory) . DIRECTORY_SEPARATOR . $filename; // Download file try { // Dummy escape to get rid of spaces to awoid 400 Bad Request $url = str_replace(' ', '%20', $url); $fh = fopen($filename, 'w'); Remote::get($url, null, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FILE => $fh)); $size = Arr::get(fstat($fh), 'size', 0); fclose($fh); // Set permissions if ($chmod !== false) { chmod($filename, $chmod); } // Build file array $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return array('error' => UPLOAD_ERR_OK, 'name' => $original_filename, 'type' => $mime, 'tmp_name' => $filename, 'size' => $size); } catch (Kohana_Exception $e) { return false; } }
public function generate($filename) { // Progress controller file $files = explode('/', $filename); $files_count = count($files); // Progress controller directory $directory = NULL; if ($files_count > 1) { // progress controller name for ($i = 0; $i < $files_count; $i++) { if ($i != $files_count - 1) { $directory .= $files[$i] . DIRECTORY_SEPARATOR; } } $filename = $files[$files_count - 1]; } // Set the name of the controller path $directory = $this->config->apppath . Terminal::VIEWPATH . $directory; if (!is_dir($directory)) { // Create the yearly directory mkdir($directory, 0777, TRUE); // Set permissions (must be manually set to fix umask issues) chmod($directory, 0777); } // Set the name of the log file $filename = $directory . $filename . EXT; if (!file_exists($filename)) { // Create the controller file file_put_contents($filename, Kohana::FILE_SECURITY . PHP_EOL); // Allow anyone to write to controller files chmod($filename, 0666); $result = Terminal::color('create', 'green'); } else { $result = Terminal::color('exist', 'blue'); } $result = ' ' . $result . ' ' . Kohana::debug_path($filename); echo $result . PHP_EOL; }
/** * Tests Kohana::debug_path() * * @test * @dataProvider provider_debug_path * @covers Kohana::debug_path * @param boolean $path Input for Kohana::debug_path * @param boolean $expected Output for Kohana::debug_path */ public function testDebugPath($path, $expected) { $this->assertEquals($expected, Kohana::debug_path($path)); }
/** * Inline exception handler, displays the error message, source of the * exception, and the stack trace of the error. * * @param object exception object * @return boolean */ public static function exception_handler(Exception $e) { try { // Get the exception information $type = get_class($e); $code = $e->getCode(); $message = $e->getMessage(); $file = $e->getFile(); $line = $e->getLine(); // Set the text version of the exception $text = "{$type} [ {$code} ]: {$message} " . self::debug_path($file) . " [ {$line} ]"; // Add this exception to the log self::$log->add(Kohana::ERROR, $text); if (Kohana::$is_cli) { // Just display the text of the exception echo "\n{$text}\n"; return TRUE; } // Get the exception backtrace $trace = $e->getTrace(); if ($e instanceof ErrorException and version_compare(PHP_VERSION, '5.3', '<')) { // Work around for a bug in ErrorException::getTrace() that exists in // all PHP 5.2 versions. @see http://bugs.php.net/bug.php?id=45895 for ($i = count($trace) - 1; $i > 0; --$i) { if (isset($trace[$i - 1]['args'])) { // Re-position the args $trace[$i]['args'] = $trace[$i - 1]['args']; // Remove the args unset($trace[$i - 1]['args']); } } } // Get the source of the error $source = self::debug_source($file, $line); // Generate a new error id $error_id = uniqid(); // Start an output buffer ob_start(); // Include the exception HTML include self::find_file('views', 'kohana/error'); // Display the contents of the output buffer echo ob_get_clean(); return TRUE; } catch (Exception $e) { // Clean the output buffer if one exists ob_get_level() and ob_clean(); // This can happen when the kohana error view has a PHP error echo $e->getMessage(), ' [ ', Kohana::debug_path($e->getFile()), ', ', $e->getLine(), ' ]'; // Exit with an error status exit(1); } }
public function generate($filename, $extends = NULL) { // Progress controller file $files = explode('/', $filename); $files_count = count($files); // Progress controller directory $model = array('Model'); $directory = NULL; if ($files_count > 1) { // progress controller name for ($i = 0; $i < $files_count; $i++) { $model[] = $this->_format_name($files[$i]); if ($i != $files_count - 1) { $directory .= $files[$i] . DIRECTORY_SEPARATOR; } } $filename = $files[$files_count - 1]; } else { $model[] = $this->_format_name($filename); } // Set the name of controller file $model = implode('_', $model); // Set controller extends if ($extends) { $parent = strtolower($extends); switch ($parent) { case 'orm': $parent = strtoupper($parent); default: $parent = ucfirst($parent); } $extends = $parent; } else { $extends = 'Model'; } // Set the name of the controller path $directory = $this->config->apppath . Terminal::MODELPATH . $directory; if (!is_dir($directory)) { // Create the yearly directory mkdir($directory, 0777, TRUE); // Set permissions (must be manually set to fix umask issues) chmod($directory, 0777); } // Set the name of the log file $filename = $directory . $filename . EXT; if (!file_exists($filename)) { // Create the controller file file_put_contents($filename, Kohana::FILE_SECURITY . PHP_EOL); // Allow anyone to write to controller files chmod($filename, 0666); // Continute to write file_put_contents($filename, PHP_EOL . 'class ' . ucfirst($model) . ' extends ' . $extends . ' {', FILE_APPEND); file_put_contents($filename, PHP_EOL . PHP_EOL . '}' . PHP_EOL . PHP_EOL, FILE_APPEND); $result = Terminal::color('create', 'green'); } else { $result = Terminal::color('exist', 'blue'); } $result = ' ' . $result . ' ' . Kohana::debug_path($filename); echo $result . PHP_EOL; }
== content default from <?php echo Kohana::debug_path(__FILE__);
/** * Get a single line of text representing the exception: * * Error [ Code ]: Message ~ File [ Line ] * * @param object Exception * @return string */ public static function exception_text(Exception $e) { return sprintf('%s [ %s ]: %s ~ %s [ %d ]', get_class($e), $e->getCode(), strip_tags($e->getMessage()), Kohana::debug_path($e->getFile()), $e->getLine()); }
/** * Magic method, returns the output of render(). If any exceptions are * thrown, the exception output will be returned instead. * * @return string */ public function __toString() { try { return $this->render(); } catch (Exception $e) { return (string) $e->getMessage() . ' in ' . Kohana::debug_path($e->getFile()) . ' [ ' . $e->getLine() . ' ]'; } }
/** * Get and return file info * * @param string path to file * @throws Kohana_Exception * @return object file info */ protected function get_info($file) { try { // Get the real path to the file $file = realpath($file); // Get the image information $info = getimagesize($file); } catch (Exception $e) { // Ignore all errors while reading the image } if (empty($file) or empty($info)) { throw new Kohana_Exception('Not an image or invalid image: :file', array(':file' => Kohana::debug_path($file))); } $return = new stdClass(); $return->file = $file; $return->width = $info[0]; $return->height = $info[1]; $return->type = $info[2]; $return->mime = image_type_to_mime_type($return->type); return $return; }
<?php } elseif ($result instanceof Exception) { ?> <td class="k-error"> <strong><?php echo __('Error'); ?> </strong><br/> <p><?php echo $result->getMessage(); ?> </p> <div><?php echo Kohana::debug_path($result->getFile()); ?> [ <?php echo $result->getLine(); ?> ]</div> <pre class="source"><code><?php echo Kohana::debug_source($result->getFile(), $result->getLine()); ?> </code></pre> </td> <?php } ?>
/** * Helper for Kohana::dump(), handles recursion in arrays and objects. * * @param mixed variable to dump * @param integer maximum length of strings * @param integer recursion level (internal) * @return string */ protected static function _dump(&$var, $length = 128, $level = 0) { if ($var === NULL) { return '<small>NULL</small>'; } elseif (is_bool($var)) { return '<small>bool</small> ' . ($var ? 'TRUE' : 'FALSE'); } elseif (is_float($var)) { return '<small>float</small> ' . $var; } elseif (is_resource($var)) { if (($type = get_resource_type($var)) === 'stream' and $meta = stream_get_meta_data($var)) { $meta = stream_get_meta_data($var); if (isset($meta['uri'])) { $file = $meta['uri']; if (function_exists('stream_is_local')) { // Only exists on PHP >= 5.2.4 if (stream_is_local($file)) { $file = Kohana::debug_path($file); } } return '<small>resource</small><span>(' . $type . ')</span> ' . htmlspecialchars($file, ENT_NOQUOTES, Kohana::$charset); } } else { return '<small>resource</small><span>(' . $type . ')</span>'; } } elseif (is_string($var)) { if (UTF8::strlen($var) > $length) { // Encode the truncated string $str = htmlspecialchars(UTF8::substr($var, 0, $length), ENT_NOQUOTES, Kohana::$charset) . ' …'; } else { // Encode the string $str = htmlspecialchars($var, ENT_NOQUOTES, Kohana::$charset); } return '<small>string</small><span>(' . strlen($var) . ')</span> "' . $str . '"'; } elseif (is_array($var)) { $output = array(); // Indentation for this variable $space = str_repeat($s = ' ', $level); static $marker; if ($marker === NULL) { // Make a unique marker $marker = uniqid(""); } if (empty($var)) { // Do nothing } elseif (isset($var[$marker])) { $output[] = "(\n{$space}{$s}*RECURSION*\n{$space})"; } elseif ($level < 5) { $output[] = "<span>("; $var[$marker] = TRUE; foreach ($var as $key => &$val) { if ($key === $marker) { continue; } if (!is_int($key)) { $key = '"' . htmlspecialchars($key, ENT_NOQUOTES, self::$charset) . '"'; } $output[] = "{$space}{$s}{$key} => " . Kohana::_dump($val, $length, $level + 1); } unset($var[$marker]); $output[] = "{$space})</span>"; } else { // Depth too great $output[] = "(\n{$space}{$s}...\n{$space})"; } return '<small>array</small><span>(' . count($var) . ')</span> ' . implode("\n", $output); } elseif (is_object($var)) { // Copy the object as an array $array = (array) $var; $output = array(); // Indentation for this variable $space = str_repeat($s = ' ', $level); $hash = spl_object_hash($var); // Objects that are being dumped static $objects = array(); if (empty($var)) { // Do nothing } elseif (isset($objects[$hash])) { $output[] = "{\n{$space}{$s}*RECURSION*\n{$space}}"; } elseif ($level < 10) { $output[] = "<code>{"; $objects[$hash] = TRUE; foreach ($array as $key => &$val) { if ($key[0] === "") { // Determine if the access is protected or protected $access = '<small>' . ($key[1] === '*' ? 'protected' : 'private') . '</small>'; // Remove the access level from the variable name $key = substr($key, strrpos($key, "") + 1); } else { $access = '<small>public</small>'; } $output[] = "{$space}{$s}{$access} {$key} => " . Kohana::_dump($val, $length, $level + 1); } unset($objects[$hash]); $output[] = "{$space}}</code>"; } else { // Depth too great $output[] = "{\n{$space}{$s}...\n{$space}}"; } return '<small>object</small> <span>' . get_class($var) . '(' . count($array) . ')</span> ' . implode("\n", $output); } else { return '<small>' . gettype($var) . '</small> ' . htmlspecialchars(print_r($var, TRUE), ENT_NOQUOTES, Kohana::$charset); } }
<?php echo $doc->description ?> <?php if ($doc->tags): ?> <dl class="tags"> <?php foreach ($doc->tags as $name => $set): ?> <dt><?php echo $name ?></dt> <?php foreach ($set as $tag): ?> <dd><?php echo $tag ?></dd> <?php endforeach ?> <?php endforeach ?> </dl> <?php endif; ?> <p class="note"> <?php if ($path = $doc->class->getFilename()): ?> Class declared in <tt><?php echo Kohana::debug_path($path) ?></tt> on line <?php echo $doc->class->getStartLine() ?>. <?php else: ?> Class is not declared in a file, it is probably an internal <?php echo html::anchor('http://php.net/manual/class.'.strtolower($doc->class->name).'.php', 'PHP class') ?>. <?php endif ?> </p> <div class="toc"> <div class="constants"> <h3><?php echo __('Constants'); ?></h3> <ul> <?php if ($doc->constants): ?> <?php foreach ($doc->constants as $name => $value): ?> <li><a href="#constant:<?php echo $name ?>"><?php echo $name ?></a></li> <?php endforeach ?> <?php else: ?> <li><em><?php echo __('None'); ?></em></li>
/** * Save the image. If the filename is omitted, the original image will * be overwritten. * * // Save the image as a PNG * $image->save('saved/cool.png'); * * // Overwrite the original image * $image->save(); * * [!!] If the file exists, but is not writable, an exception will be thrown. * * [!!] If the file does not exist, and the directory is not writable, an * exception will be thrown. * * @param string new image path * @param integer quality of image: 1-100 * @return boolean * @uses Image::_save * @throws Kohana_Exception */ public function save($file = NULL, $quality = 100) { if ($file === NULL) { // Overwrite the file $file = $this->file; } if (is_file($file)) { if (!is_writable($file)) { throw new Kohana_Exception('File must be writable: :file', array(':file' => Kohana::debug_path($file))); } } else { // Get the directory of the file $directory = realpath(pathinfo($file, PATHINFO_DIRNAME)); if (!is_dir($directory) or !is_writable($directory)) { throw new Kohana_Exception('Directory must be writable: :directory', array(':directory' => Kohana::debug_path($directory))); } } // The quality must be in the range of 1 to 100 $quality = min(max($quality, 1), 100); return $this->_do_save($file, $quality); }
<div id="kohana_error_<?php echo $error_id; ?> " class="kohana_error" style="display:block;position:relative;z-index:1000;background:#cff292;font-size:1em;font-family:sans-serif;text-align:left"> <div class="message" style="display:block;margin:0;padding:1em;color:#111"> <p class="text" style="display:block;margin:0;padding:0;color:#111"><a href="#toggle_details" class="toggle_details" style="padding:0color:#39530c;text-decoration:underline;background:transparent" onclick="javascript:<?php echo $toggle_details; ?> "><?php echo $type, ' [ ', $code, ' ]'; ?> </a>: <code style="display:inline;margin:0;padding:0;font-size:1em;background:transparent:color:#111;font-family:sans-serif"><?php echo $message; ?> </code> <span class="file"><?php echo Kohana::debug_path($file), ' [ ', $line, ' ]'; ?> </span></p> </div> <div id="kohana_error_<?php echo $error_id; ?> _details" class="details" style="display:none;margin:0;padding:0 1em 1em;color:#111"> <pre class="source" style="display:block;margin:0;padding:1em;font-family:monospace;background:#efefef;color:#111"><?php echo $source; ?> </pre> <ol class="trace" style="display:block;margin:0 2em 0;padding:1em 0 0;color:#111"><?php foreach (Kohana::trace($trace) as $step) { ?> <li style="padding:0 0 0.8em;line-height:1.3em"><code style="font-size:1.2em"><?php
<?php echo $doc->description; ?> <?php if ($doc->tags) { echo View::factory('userguide/api/tags')->set('tags', $doc->tags); } ?> <p class="note"> <?php if ($path = $doc->class->getFilename()) { ?> Class declared in <tt><?php echo Kohana::debug_path($path); ?> </tt> on line <?php echo $doc->class->getStartLine(); ?> . <?php } else { ?> Class is not declared in a file, it is probably an internal <?php echo html::anchor('http://php.net/manual/class.' . strtolower($doc->class->name) . '.php', 'PHP class'); ?> . <?php } ?>
/** * Returns an array of HTML strings that represent each step in the backtrace. * * // Displays the entire current backtrace * echo implode('<br/>', Kohana::trace()); * * @param string path to debug * @return string */ public static function trace(array $trace = NULL) { if ($trace === NULL) { // Start a new trace $trace = debug_backtrace(); } // Non-standard function calls $statements = array('include', 'include_once', 'require', 'require_once'); $output = array(); foreach ($trace as $step) { if (!isset($step['function'])) { // Invalid trace step continue; } if (isset($step['file']) and isset($step['line'])) { // Include the source of this step $source = Kohana::debug_source($step['file'], $step['line']); } if (isset($step['file'])) { $file = $step['file']; if (isset($step['line'])) { $line = $step['line']; } } // function() $function = $step['function']; if (in_array($step['function'], $statements)) { if (empty($step['args'])) { // No arguments $args = array(); } else { // Sanitize the file path $args = array(Kohana::debug_path($step['args'][0])); } } elseif (isset($step['args'])) { if (!function_exists($step['function']) or strpos($step['function'], '{closure}') !== FALSE) { // Introspection on closures or language constructs in a stack trace is impossible $params = NULL; } else { if (isset($step['class'])) { if (method_exists($step['class'], $step['function'])) { $reflection = new ReflectionMethod($step['class'], $step['function']); } else { $reflection = new ReflectionMethod($step['class'], '__call'); } } else { $reflection = new ReflectionFunction($step['function']); } // Get the function parameters $params = $reflection->getParameters(); } $args = array(); foreach ($step['args'] as $i => $arg) { if (isset($params[$i])) { // Assign the argument by the parameter name $args[$params[$i]->name] = $arg; } else { // Assign the argument by number $args[$i] = $arg; } } } if (isset($step['class'])) { // Class->method() or Class::method() $function = $step['class'] . $step['type'] . $step['function']; } $output[] = array('function' => $function, 'args' => isset($args) ? $args : NULL, 'file' => isset($file) ? $file : NULL, 'line' => isset($line) ? $line : NULL, 'source' => isset($source) ? $source : NULL); unset($function, $args, $file, $line, $source); } return $output; }
?> </a> (<?php echo count($included); ?> )</h3> <div id="<?php echo $env_id; ?> " class="collapsed"> <table cellspacing="0"> <?php foreach ($included as $file) { ?> <tr> <td><code><?php echo Kohana::debug_path($file); ?> </code></td> </tr> <?php } ?> </table> </div> <?php foreach (array('_SESSION', '_GET', '_POST', '_FILES', '_COOKIE', '_SERVER') as $var) { ?> <?php if (empty($GLOBALS[$var]) or !is_array($GLOBALS[$var])) { continue; }
/** * * @param string $path_to_original path to image file */ public function save_image($input_file) { // Try to regenerate if ($input_file === TRUE) { // We need biggest picture. $file = $this->path(FALSE, TRUE); } else { // Otherviwe we add new Image $file = $input_file; } if (!is_file($file)) { throw new Exception_Thumbler_File_Found('File :file not found', array(':file' => Kohana::debug_path($file))); } if (!is_readable($file)) { throw new Exception_Thumbler_File_Read('File :file can not be readed', array(':file' => Kohana::debug_path($file))); } try { $original = Image::factory($file); } catch (Exception $e) { $original = NULL; } if (!$original) { throw new Exception_Thumbler_File_Error('File :file contain some errors and can not be loaded', array(':file' => Kohana::debug_path($file))); } if (!empty($this->config['max_width']) and $original->width > $this->config['max_width']) { throw new Exception_Thumbler_Max_Width('Image width is too big'); } if (!empty($this->config['max_height']) and $original->height > $this->config['max_height']) { throw new Exception_Thumbler_Max_Width('Image height is too big'); } if (!empty($this->config['max_square']) and $original->width * $original->height > $this->config['max_square']) { throw new Exception_Thumbler_Max_Width('Image width is too big'); } $dir = $this->path . DIRECTORY_SEPARATOR . $this->directory(TRUE); if (!is_dir($dir)) { try { if (!mkdir($dir, 0777, TRUE)) { throw new Exception(); } } catch (Exception $e) { throw new Exception_Thumbler_File_Write('Can not create :dir', array(':dir' => Kohana::debug_path($dir))); } } if (!is_writable($dir)) { throw new Exception_Thumbler_File_Write('Can not write in :dir', array(':dir' => Kohana::debug_path($dir))); } // Config $image_config = $this->get_config(); if ($input_file !== TRUE) { // process main image only for new image $this->_process_image_size($original, $image_config['default_size']); } $image_sizes = array_keys($image_config['sizes']); // Exclude default size from size list becouse it already processed unset($image_sizes[array_search($image_config['default_size'], $image_sizes)]); foreach ($image_sizes as $size) { $this->_process_image_size(clone $original, $size); } }