/**
	 * Returns a detailed error message.
	 *
	 * The error message contains:
	 * 1. Error Number (only if greater than zero)
	 * 2. Error Message
	 * 3. File Name
	 * 4. Line Number of the file
	 * 5. Executed SQL Query (optional)
	 *
	 * @return string All error details as string
	 */
	public function __toString() {
		$error = parent::__toString();
		if (empty($this->query) == false) {
			$query = Strings::replaceLineBreaks($this->query, "\t");
			$error .= "\r\nQuery: {$query}";
		}
		return $error;
	}
Пример #2
0
	/**
	 * @dataProvider providerReplaceLineBreaks
	 */
	public function testReplaceLineBreaks($param_string, $param_replace, $expected) {
		$result = Strings::replaceLineBreaks($param_string, $param_replace);
		$this->assertEquals(
			$expected,
			$result,
			"Could not properly replace line breaks with ".var_export($param_replace, true).
				" in string ".var_export($param_string, true)
		);
	}
	/**
	 * Transforms a multidimensional array (like arrays returned by FileINI::parse()) to a string.
	 *
	 * This function writes sections and keys case sensitive. Invalid characters are converted to
	 * ASCII dash/hyphen (-). Values are always enclosed by double-quotes. All line breaks are
	 * translated to CRLF.
	 *
	 * You can specify a comment to prepend before the file. This would overwrite a comment that
	 * was read before from the file. For a single line comment you can just specify a string that
	 * will be used, for a multiline comment you have to put each line into an array element. The
	 * comment must not contain line breaks, but if there are any, they will be replaced with a
	 * white space. If you specify an empty array no comment will be written, if you specify null
	 * (default) the comment read from the file will be used (if any). The comment char specified
	 * before ( FileINI::setCommentChar() ) will be used.
	 *
	 * @param array The data to transform
	 * @param mixed Comment to prepend before the file
	 * @return string The transformed data as CSV
	 * @author Justin Frim <*****@*****.**>
	 */
	public function transformArray(array $array, $comment = null) {
		// Handle comment parameter
		if (is_array($comment)) {
			$this->comment = $comment;
		}
		elseif ($comment !== null && !is_array($comment)) {
			$this->comment = array($comment);
		}

		$data = '';
		// Transform comment and add to ini
		if (count($this->comment) > 0) {
			foreach ($this->comment as $comtext) {
				$data .= $this->commentChar . Strings::replaceLineBreaks($comtext, ' ') . self::CRLF;
			}
		}

		// Transform data and add to ini
		foreach ($array as $sections => $items) {
			//Write the section
			if (isset($section)) {
				$data .= self::CRLF;
			}
			if (is_array($items)) {
				// Remove invalid chars from section name (\0-\37 is octal notation for ascii chars)
				$section = preg_replace('/[\0-\37]|\177/', "-", $sections);
				$data .= "[{$section}]" . self::CRLF;
				foreach ($items as $keys => $values) {
					// Remove invalid chars from key name
					$key = preg_replace('/[\0-\37]|=|\177/', "-", $keys);
					// Replace comment char at the beginning
		  			if (substr($key, 0, 1) == $this->commentChar) {
		  				$key = '-'.substr($key, 1);
		  			}
			  		$values = str_replace("\r", '\r', $values);
			  		$values = str_replace("\n", '\n', $values);
					if ($this->mode == self::BINARY) {
						$value = addcslashes($values, '');
					}
					// Will be ' keyname = "value"\r\n' (without ')
		  			$data .= ' ' . $key . ' = "' . $value . '"' . self::CRLF;
				}
			}
			else {
				$key = preg_replace('/[\0-\37]|=|\177/', "-", $sections);
		  		if (substr($key, 0, 1) == $this->commentChar) {
		  			$key = '-'.substr($key, 1);
		  		}
		  		$items = str_replace("\r", '\r', $items);
		  		$items = str_replace("\n", '\n', $items);
				if ($this->mode == self::BINARY) {
					$value = addcslashes($items, '');
				}
				// Will be 'keyname = "value"\r\n' (without ')
		  		$data .= $key . ' = "' . $value . '"' . self::CRLF;
			}
	  	}
		return $data;
	}
	protected function _data_write_block($mode, $block) {
		if($mode != self::BINARY) {
			$block = Strings::replaceLineBreaks($block, $this->eol_code[$this->OS_remote]);
		}
		do {
			if(($t = @socket_write($this->ftp_temp_sock, $block)) === false) {
				$socketErr =  socket_strerror(socket_last_error($this->ftp_temp_sock));
				$this->pushError("_data_write", "socket_write", $ocketErr);
				$this->_data_close();
				return false;
			}
			$block = substr($block, $t);
		} while(!empty($block));
		return true;
	}
	/**
	 * Changes every end of line from CR or LF to CRLF.
	 * @access private
	 * @return string
	 */
	private function FixEOL($str) {
		return Strings::replaceLineBreaks($str, $this->LE);
	}
	/**
	 * Returns the create statement for creating the specified table.
	 *
	 * Set the second parameter to true to add "DROP TABLE IF EXISTS" statements before the
	 * create statement.
	 *
	 * After executing this method the MySQL option "SQL_QUOTE_SHOW_CREATE" is set to 1.
	 *
	 * @param string Name of the table
	 * @param boolean Add drop statements (default: false, no drop statements)
	 * @return string Create statement or null
	 */
	public function getStructure($table, $drop = false) {
		// Activate Quotes in sql names
		$this->rawQuery('SET SQL_QUOTE_SHOW_CREATE = 1');

		$table_data = '';
		if ($drop == true) {
			$table_data .= 'DROP TABLE IF EXISTS '.chr(96).$table.chr(96).';' ."\n";
		}
		$result = $this->rawQuery('SHOW CREATE TABLE '.chr(96).$table.chr(96));
		$show_results = $this->fetchNum($result);
		if (!$this->isResultSet($show_results)) {
			return false;
		}
		else {
			$table_data .= Strings::replaceLineBreaks($show_results[1], "\n").';';
			return trim($table_data);
		}
	}
	protected function _data_write_block($mode, $block) {
		if($mode != self::BINARY) {
			$block = Strings::replaceLineBreaks($block, $this->eol_code[$this->OS_remote]);
		}

		do {
			if(($t = @fwrite($this->ftp_data_sock, $block)) === false) {
				$this->pushError("_data_write", "Can't write to socket");
				return false;
			}
			$block = substr($block, $t);
		} while(!empty($block));

		return true;
	}
Пример #8
0
	/**
	 * Text to add to the logfile.
	 *
	 * Line breaks, carriage returns and tabs will be converted to a single space char.
	 *
	 * @param string Text to write to the logfile
	 */
	public function addText($text) {
		$text = str_replace("\t", " ", $text);
		$text = Strings::replaceLineBreaks($text, " ");
		$this->logs[] = '['.gmdate('r').'] '.$text;
	}
Пример #9
0
	/**
	 * Issues a data command and sends the msg_data to the server
	 * finializing the mail transaction. $msg_data is the message
	 * that is to be send with the headers. Each header needs to be
	 * on a single line followed by a <CRLF> with the message headers
	 * and the message body being seperated by and additional <CRLF>.
	 *
	 * Implements rfc 821: DATA <CRLF>
	 *
	 * SMTP CODE INTERMEDIATE: 354
	 *     [data]
	 *     <CRLF>.<CRLF>
	 *     SMTP CODE SUCCESS: 250
	 *     SMTP CODE FAILURE: 552,554,451,452
	 * SMTP CODE FAILURE: 451,554
	 * SMTP CODE ERROR  : 500,501,503,421
	 * @access public
	 * @return bool
	 */
	public function Data($msg_data) {
		$this->error = null; // so no confusion is caused

		if (!$this->connected()) {
			$this->error = array("error" => "Called Data() without being connected");
			return false;
		}

		fputs($this->smtp_conn, "DATA".$this->CRLF);

		$rply = $this->get_lines();
		$code = substr($rply, 0, 3);

		if ($this->do_debug >= 2) {
			echo "SMTP -> FROM SERVER:".$rply.$this->CRLF.'<br />';
		}

		if ($code != 354) {
			$this->error = array(
				"error" => "DATA command not accepted from server",
				"smtp_code" => $code,
				"smtp_msg" => substr($rply, 4)
			);
			if ($this->do_debug >= 1) {
				echo "SMTP -> ERROR: ".$this->error["error"].": ".$rply.$this->CRLF.'<br />';
			}
			return false;
		}

		/* the server is ready to accept data!
		 * according to rfc 821 we should not send more than 1000
		 * including the CRLF
		 * characters on a single line so we will break the data up
		 * into lines by \r and/or \n then if needed we will break
		 * each of those into smaller lines to fit within the limit.
		 * in addition we will be looking for lines that start with
		 * a period '.' and append and additional period '.' to that
		 * line. NOTE: this does not count towards limit.
		 */

		// normalize the line breaks so we know the explode works
		$msg_data = Strings::replaceLineBreaks($msg_data, "\n");
		$lines = explode("\n", $msg_data);

		/* we need to find a good way to determine is headers are
		 * in the msg_data or if it is a straight msg body
		 * currently I am assuming rfc 822 definitions of msg headers
		 * and if the first field of the first line (':' sperated)
		 * does not contain a space then it _should_ be a header
		 * and we can process all lines before a blank "" line as
		 * headers.
		 */

		$field = substr($lines[0], 0, strpos($lines[0], ":"));
		$in_headers = false;
		if (!empty($field) && !strstr($field, " ")) {
			$in_headers = true;
		}

		$max_line_length = 998; // used below; set here for ease in change

		while (list(, $line) = @each($lines)) {
			$lines_out = null;
			if ($line == "" && $in_headers) {
				$in_headers = false;
			}
			// ok we need to break this line up into several smaller lines
			while (strlen($line) > $max_line_length) {
				$pos = strrpos(substr($line, 0, $max_line_length), " ");

				// Patch to fix DOS attack
				if (!$pos) {
					$pos = $max_line_length - 1;
					$lines_out[] = substr($line, 0, $pos);
					$line = substr($line, $pos);
				}
				else {
					$lines_out[] = substr($line, 0, $pos);
					$line = substr($line, $pos + 1);
				}

				/* if processing headers add a LWSP-char to the front of new line
				 * rfc 822 on long msg headers
				 */
				if ($in_headers) {
					$line = "\t".$line;
				}
			}
			$lines_out[] = $line;

			// send the lines to the server
			while (list(, $line_out) = @each($lines_out)) {
				if (strlen($line_out) > 0) {
					if (substr($line_out, 0, 1) == ".") {
						$line_out = ".".$line_out;
					}
				}
				fputs($this->smtp_conn, $line_out.$this->CRLF);
			}
		}

		// message data has been sent
		fputs($this->smtp_conn, $this->CRLF.".".$this->CRLF);

		$rply = $this->get_lines();
		$code = substr($rply, 0, 3);

		if ($this->do_debug >= 2) {
			echo "SMTP -> FROM SERVER:".$rply.$this->CRLF.'<br />';
		}

		if ($code != 250) {
			$this->error = array(
				"error" => "DATA not accepted from server",
				"smtp_code" => $code,
				"smtp_msg" => substr($rply, 4)
			);
			if ($this->do_debug >= 1) {
				echo "SMTP -> ERROR: ".$this->error["error"].": ".$rply.$this->CRLF.'<br />';
			}
			return false;
		}
		return true;
	}