/** * @param \Exception $exception * @return string * @todo Fix this to get full stack trace */ public static function getExceptionTraceAsString(\Exception $exception) { $ret = ""; $count = 0; foreach ($exception->getTrace() as $trace) { $args = ""; if (isset($trace['args'])) { $args = array(); foreach ($trace['args'] as $arg) { if (is_string($arg)) { $args[] = "'" . $arg . "'"; } elseif (is_array($arg)) { $args[] = "Array"; } elseif (is_null($arg)) { $args[] = 'NULL'; } elseif (is_bool($arg)) { $args[] = $arg ? "true" : "false"; } elseif (is_object($arg)) { $args[] = get_class($arg); } elseif (is_resource($arg)) { $args[] = get_resource_type($arg); } else { $args[] = $arg; } } $args = join(", ", $args); } $ret .= sprintf("#%s %s(%s): %s(%s)\n", $count, isset($trace['file']) ? $trace['file'] : 'unknown file', isset($trace['line']) ? $trace['line'] : 'unknown line', isset($trace['class']) ? $trace['class'] . $trace['type'] . $trace['function'] : $trace['function'], $args); $count++; } return $ret; }
private function varToString($var) { if (is_object($var)) { return sprintf('Object(%s)', get_class($var)); } if (is_array($var)) { $a = array(); foreach ($var as $k => $v) { $a[] = sprintf('%s => %s', $k, $this->varToString($v)); } return sprintf('Array(%s)', implode(', ', $a)); } if (is_resource($var)) { return sprintf('Resource(%s)', get_resource_type($var)); } if (null === $var) { return 'null'; } if (false === $var) { return 'false'; } if (true === $var) { return 'true'; } return (string) $var; }
public function createOutputFile() { if (!is_resource($this->getFileStream())) { $this->setFileStream(fopen($this->createFileName(), "w+")); fwrite($this->getFileStream(), $this->header()); } }
public function obtenerArreglo($result) { if (!is_resource($result)) { return false; } return pg_fetch_array($result); }
public function testOpeningFileWorks() { $this->assertFalse($this->instance->fileExists('testfile')); $this->instance->setFileContents('testfile', 'mmmmm plastic'); $this->assertTrue($this->instance->fileExists('testfile')); $this->assertTrue(is_resource($this->instance->openFile('testfile'))); }
/** * Process * * @param mixed $value * @param array $options * * @return string|null */ public function process($value, array $options = array()) { if ($value === null) { return; } if (!class_exists('\\libphonenumber\\PhoneNumberUtil')) { throw new \RuntimeException('Library for phone checking not found'); } if (is_object($value) || is_resource($value) || is_array($value)) { $this->throwException($options, 'error'); } try { $phone = $this->getPhone($value, $options, $is_toll_free); } catch (\libphonenumber\NumberParseException $e) { $this->throwException($options, 'error'); } $util = \libphonenumber\PhoneNumberUtil::getInstance(); if (!$util->isValidNumber($phone)) { $this->throwException($options, 'error'); } if ($is_toll_free) { return $phone->getNationalNumber(); } return (string) $util->format($phone, $this->getDefaultPhoneFormat($options)); }
/** * Returns all certificates trusted by the user * * @return \OCP\ICertificate[] */ public function listCertificates() { if (!$this->config->getSystemValue('installed', false)) { return array(); } $path = $this->getPathToCertificates() . 'uploads/'; if (!$this->view->is_dir($path)) { return array(); } $result = array(); $handle = $this->view->opendir($path); if (!is_resource($handle)) { return array(); } while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { try { $result[] = new Certificate($this->view->file_get_contents($path . $file), $file); } catch (\Exception $e) { } } } closedir($handle); return $result; }
/** * Constructor * @param resource $resource * @throws \InvalidArgumentException If invalid resource */ public function __construct($resource) { if (!is_resource($resource)) { throw new \InvalidArgumentException('Cannot create LogWriter. Invalid resource handle.'); } $this->resource = $resource; }
/** * Serialize the data for storing. * * Serializes the given $data to a executable PHP code representation * string. This works with objects implementing {@link ezcBaseExportable}, * arrays and scalar values (int, bool, float, string). The return value is * executable PHP code to be stored to disk. The data can be unserialized * using the {@link fetchData()} method. * * @param mixed $data * @return string * * @throws ezcCacheInvalidDataException * if the $data can not be serialized (e.g. an object that does not * implement ezcBaseExportable, a resource, ...). */ protected function prepareData($data) { if (is_object($data) && !$data instanceof ezcBaseExportable || is_resource($data)) { throw new ezcCacheInvalidDataException(gettype($data), array('simple', 'array', 'ezcBaseExportable')); } return "<?php\nreturn " . var_export($data, true) . ";\n?>\n"; }
public function query($sql) { $resource = mysql_query($sql, $this->link); if ($resource) { if (is_resource($resource)) { $i = 0; $data = array(); while ($result = mysql_fetch_assoc($resource)) { $data[$i] = $result; $i++; } mysql_free_result($resource); $query = new stdClass(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; unset($data); return $query; } else { return true; } } else { trigger_error('Error: ' . mysql_error($this->link) . '<br />Error No: ' . mysql_errno($this->link) . '<br />' . $sql); exit; } }
/** * {@inheritdoc} */ protected function write(array $record) { if (!is_resource($this->stream)) { if (!$this->url) { throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); } $this->createDir(); $this->errorMessage = null; set_error_handler(array($this, 'customErrorHandler')); $this->stream = fopen($this->url, 'a'); if ($this->filePermission !== null) { @chmod($this->url, $this->filePermission); } restore_error_handler(); if (!is_resource($this->stream)) { $this->stream = null; throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: ' . $this->errorMessage, $this->url)); } } if ($this->useLocking) { // ignoring errors here, there's not much we can do about them flock($this->stream, LOCK_EX); } fwrite($this->stream, (string) $record['formatted']); if ($this->useLocking) { flock($this->stream, LOCK_UN); } }
public function __destruct() { if (\is_resource($this->filePointer)) { $this->writeLocationTable(); \fclose($this->filePointer); } }
public static function serializeValue($value) { if ($value === null) { return 'null'; } elseif ($value === false) { return 'false'; } elseif ($value === true) { return 'true'; } elseif (is_float($value) && (int) $value == $value) { return $value . '.0'; } elseif (is_object($value) || gettype($value) == 'object') { return 'Object ' . get_class($value); } elseif (is_resource($value)) { return 'Resource ' . get_resource_type($value); } elseif (is_array($value)) { return 'Array of length ' . count($value); } elseif (is_integer($value)) { return (int) $value; } else { $value = (string) $value; if (function_exists('mb_convert_encoding')) { $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8'); } return $value; } }
function realFilesize($filename) { $fp = fopen($filename, 'r'); $return = false; if (is_resource($fp)) { if (PHP_INT_SIZE < 8) { if (0 === fseek($fp, 0, SEEK_END)) { $return = 0.0; $step = 0x7fffffff; while ($step > 0) { if (0 === fseek($fp, -$step, SEEK_CUR)) { $return += floatval($step); } else { $step >>= 1; } } } } else { if (0 === fseek($fp, 0, SEEK_END)) { $return = ftell($fp); } } } return $return; }
private static function query($q, $params = array()) { if (self::$link === NULL) { self::connect(); } self::$numQuerys++; $q .= self::$order; $q .= self::$limit; self::$order = ''; self::$limit = ''; self::$sql = $q; self::$result = mysql_query($q, self::$link); if (!self::$result) { return false; } else { if (!is_resource(self::$result)) { return true; } } $rset = array(); while ($row = mysql_fetch_assoc(self::$result)) { $rset[] = $row; } return $rset; }
/** * Constructor method * * @param string|resource $input is either a stream or a filename * @param int $size see $_FILES['size'] from PHP * @param int $errorStatus see $_FILES['error'] * @param string $clientFilename the original filename handed over from the client * @param string $clientMediaType the media type (optional) * * @throws \InvalidArgumentException */ public function __construct($input, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) { if (is_string($input)) { $this->file = $input; } if (is_resource($input)) { $this->stream = new Stream($input); } elseif ($input instanceof StreamInterface) { $this->stream = $input; } if (!$this->file && !$this->stream) { throw new \InvalidArgumentException('The input given was not a valid stream or file.', 1436717301); } if (!is_int($size)) { throw new \InvalidArgumentException('The size provided for an uploaded file must be an integer.', 1436717302); } $this->size = $size; if (!is_int($errorStatus) || 0 > $errorStatus || 8 < $errorStatus) { throw new \InvalidArgumentException('Invalid error status for an uploaded file. See UPLOAD_ERR_* constant in PHP.', 1436717303); } $this->error = $errorStatus; if ($clientFilename !== null && !is_string($clientFilename)) { throw new \InvalidArgumentException('Invalid client filename provided for an uploaded file.', 1436717304); } $this->clientFilename = $clientFilename; if ($clientMediaType !== null && !is_string($clientMediaType)) { throw new \InvalidArgumentException('Invalid client media type provided for an uploaded file.', 1436717305); } $this->clientMediaType = $clientMediaType; }
/** * constructor * * If no file pointer given it will fall back to STDOUT. * * @param resource $out optional * @throws InvalidArgumentException */ public function __construct($out = STDOUT) { if (is_resource($out) === false || get_resource_type($out) !== 'stream') { throw new InvalidArgumentException('Given filepointer is not a resource of type stream'); } $this->out = $out; }
public function query($sql) { if ($this->link) { $resource = mysql_query($sql, $this->link); if ($resource) { if (is_resource($resource)) { $i = 0; $data = array(); while ($result = mysql_fetch_assoc($resource)) { $data[$i] = $result; $i++; } mysql_free_result($resource); $query = new \stdClass(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; unset($data); return $query; } else { return true; } } else { $trace = debug_backtrace(); trigger_error('Error: ' . mysql_error($this->link) . '<br />Error No: ' . mysql_errno($this->link) . '<br /> Error in: <b>' . $trace[1]['file'] . '</b> line <b>' . $trace[1]['line'] . '</b><br />' . $sql); } } }
function execute($cmd, &$output, &$error, &$returnCode) { $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $process = proc_open($cmd, $descriptorspec, $pipes); if (!is_resource($process)) { throw new RuntimeException("Unable to execute the command. [{$cmd}]"); } stream_set_blocking($pipes[1], false); stream_set_blocking($pipes[2], false); $output = $error = ''; foreach ($pipes as $key => $pipe) { while (!feof($pipe)) { if (!($line = fread($pipe, 128))) { continue; } if (1 == $key) { $output .= $line; // stdout } else { $error .= $line; // stderr } } fclose($pipe); } $returnCode = proc_close($process); }
/** Returns an array. Element 0 - GD resource. Element 1 - width. Element 2 - height. * Returns FALSE on failure. The only one parameter $image can be an instance of this class, * a GD resource, an array(width, height) or path to image file. * @param mixed $image * @return array */ protected function build_image($image) { if ($image instanceof gd) { $width = $image->get_width(); $height = $image->get_height(); $image = $image->get_image(); } elseif (is_resource($image) && get_resource_type($image) == "gd") { $width = @imagesx($image); $height = @imagesy($image); } elseif (is_array($image)) { list($key, $width) = each($image); list($key, $height) = each($image); $image = imagecreatetruecolor($width, $height); } elseif (false !== (list($width, $height, $type) = @getimagesize($image))) { $image = $type == IMAGETYPE_GIF ? @imagecreatefromgif($image) : ($type == IMAGETYPE_WBMP ? @imagecreatefromwbmp($image) : ($type == IMAGETYPE_JPEG ? @imagecreatefromjpeg($image) : ($type == IMAGETYPE_JPEG2000 ? @imagecreatefromjpeg($image) : ($type == IMAGETYPE_PNG ? imagecreatefrompng($image) : ($type == IMAGETYPE_XBM ? @imagecreatefromxbm($image) : false))))); if ($type == IMAGETYPE_PNG) { imagealphablending($image, false); } } $return = is_resource($image) && get_resource_type($image) == "gd" && isset($width) && isset($height) && preg_match('/^[1-9][0-9]*$/', $width) !== false && preg_match('/^[1-9][0-9]*$/', $height) !== false ? array($image, $width, $height) : false; if ($return !== false && isset($type)) { $this->type = $type; } return $return; }
/** * Returns `true` if value is of the specified type * * @param string $type * @param mixed $value * @return bool */ protected function checkType($type, $value) { switch ($type) { case 'array': return is_array($value); case 'bool': case 'boolean': return is_bool($value); case 'callable': return is_callable($value); case 'float': case 'double': return is_float($value); case 'int': case 'integer': return is_int($value); case 'null': return is_null($value); case 'numeric': return is_numeric($value); case 'object': return is_object($value); case 'resource': return is_resource($value); case 'scalar': return is_scalar($value); case 'string': return is_string($value); case 'mixed': return true; default: return $value instanceof $type; } }
function Open($ArchFile, $UseIncludePath = false) { // Open the zip archive if (!isset($this->Meth8Ok)) { $this->__construct(); } // for PHP 4 compatibility $this->Close(); // close handle and init info $this->Error = false; $this->ArchIsNew = false; $this->ArchIsStream = is_resource($ArchFile) && get_resource_type($ArchFile) == 'stream'; if ($this->ArchIsStream) { $this->ArchFile = 'from_stream.zip'; $this->ArchHnd = $ArchFile; } else { // open the file $this->ArchFile = $ArchFile; $this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath); } $ok = !($this->ArchHnd === false); if ($ok) { $ok = $this->CentralDirRead(); } return $ok; }
function _initialize() { $OLEfile = $this->_OLEfilename; /* Check for a filename. Workbook.pm will catch this first. */ if ($OLEfile == '') { trigger_error("Filename required", E_USER_ERROR); } /* * If the filename is a resource it is assumed that it is a valid * filehandle, if not we create a filehandle. */ if (is_resource($OLEfile)) { $fh = $OLEfile; } else { // Create a new file, open for writing $fh = fopen($OLEfile, "wb"); // The workbook class also checks this but something may have // happened since then. if (!$fh) { trigger_error("Can't open {$OLEfile}. It may be in use or " . "protected", E_USER_ERROR); } $this->_internal_fh = 1; } // Store filehandle $this->_filehandle = $fh; }
/** * Initialize curl and close the connection if * needed. */ public function open() { if (is_resource($this->curl)) { $this->close(); } $this->curl = curl_init(); }
function downloadToString() { $crlf = "\r\n"; // generate request $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf . 'Host: ' . $this->_host . $crlf . $crlf; // fetch $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port); fwrite($this->_fp, $req); while (is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) { $response .= fread($this->_fp, 1024); } fclose($this->_fp); // split header and body $pos = strpos($response, $crlf . $crlf); if ($pos === false) { return $response; } $header = substr($response, 0, $pos); $body = substr($response, $pos + 2 * strlen($crlf)); // parse headers $headers = array(); $lines = explode($crlf, $header); foreach ($lines as $line) { if (($pos = strpos($line, ':')) !== false) { $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1)); } } // redirection? if (isset($headers['location'])) { $http = new ilHttpRequest($headers['location']); return $http->DownloadToString($http); } else { return $body; } }
protected function normalize($data) { if (null === $data || is_scalar($data)) { return $data; } if (is_array($data) || $data instanceof \Traversable) { $normalized = array(); $count = 1; foreach ($data as $key => $value) { if ($count++ >= 1000) { $normalized['...'] = 'Over 1000 items, aborting normalization'; break; } $normalized[$key] = $this->normalize($value); } return $normalized; } if ($data instanceof \DateTime) { return $data->format($this->dateFormat); } if (is_object($data)) { if ($data instanceof Exception) { return $this->normalizeException($data); } return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true)); } if (is_resource($data)) { return '[resource]'; } return '[unknown(' . gettype($data) . ')]'; }
/** * Iterator function. Returns a rowset if called without parameter, * the fields contents if a field is specified or FALSE to indicate * no more rows are available. * * @param string field default NULL * @return var */ public function next($field = NULL) { if (!is_resource($this->handle) || FALSE === ($row = mssql_fetch_assoc($this->handle))) { return FALSE; } foreach ($row as $key => $value) { if (NULL === $value || !isset($this->fields[$key])) { continue; } switch ($this->fields[$key]) { case 'datetime': $row[$key] = Date::fromString($value, $this->tz); break; case 'numeric': if (FALSE !== strpos($value, '.')) { settype($row[$key], 'double'); break; } // Fallthrough intentional case 'int': if ($value <= LONG_MAX && $value >= LONG_MIN) { settype($row[$key], 'integer'); } else { settype($row[$key], 'double'); } break; } } if ($field) { return $row[$field]; } else { return $row; } }
public function affectedRows() { if (!is_resource($this->result)) { return false; } return pg_affected_rows($this->result); }
/** * Cleanup */ protected function close() { if (is_resource($this->curl)) { curl_close($this->curl); $this->curl = null; } }
function MagpieRSS($source) { # if PHP xml isn't compiled in, die # if (!function_exists('xml_parser_create')) { trigger_error("Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php"); } $parser = @xml_parser_create(); if (!is_resource($parser)) { trigger_error("Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php"); } $this->parser = $parser; # pass in parser, and a reference to this object # set up handlers # xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, 'feed_start_element', 'feed_end_element'); xml_set_character_data_handler($this->parser, 'feed_cdata'); $status = xml_parse($this->parser, $source); if (!$status) { $errorcode = xml_get_error_code($this->parser); if ($errorcode != XML_ERROR_NONE) { $xml_error = xml_error_string($errorcode); $error_line = xml_get_current_line_number($this->parser); $error_col = xml_get_current_column_number($this->parser); $errormsg = "{$xml_error} at line {$error_line}, column {$error_col}"; $this->error($errormsg); } } xml_parser_free($this->parser); $this->normalize(); }