/** * Write the message to the log file if the log level is appropriate. * * @param string $message * @param int $log_level * @return bool|int * @uses Log::$log_level * @uses Log::$file * @uses Log::$date_format * @uses Log::$separator * @uses get_string() */ public function write($message = '', $log_level = Log::LOG_LEVEL_SYSTEM_INFORMATION) { // input validation if (!Helpers::is_string_ne($message)) { return false; } if (func_num_args() === 3) { $value = func_get_arg(2); // check for value and convert it to a string for writing if (isset($value)) { // convert $value to string $value = Helpers::get_string($value); // remove HTML line breaks from log message $value = str_replace(array("<br />\n", '<br />', ' '), array("\n", "\n", ' '), $value); $message = $message . ': ' . $value; } } if ($this->log_level <= $log_level && Helpers::is_string_ne($this->file)) { // get call string from backtrace $call_string = Helpers::get_call_string(); // build the message $message = date($this->date_format) . $this->separator . $call_string . $this->separator . $message; $this->messages[] = $message; // write the message to the provided log file //return file_put_contents($this->log_directory . $this->file, $message . PHP_EOL, FILE_APPEND); return fwrite($this->handle, $message . PHP_EOL); } return true; }
/** * Bind parameters to execute. * * @param array $parameters * @return bool * @uses \PDOStatement::bindValue() */ private function bind($parameters = array()) { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); if (!Helpers::is_array_ne($parameters)) { $this->Log->write('parameters is empty', Log::LOG_LEVEL_WARNING, Helpers::get_call_string()); return false; } $this->Log->write('parameters: ' . implode(', ', $parameters), Log::LOG_LEVEL_USER); // make sure the array has numeric indexes $parameters = array_values($parameters); try { foreach ($parameters as $i => $value) { // parameters are 1-based $this->stmt->bindValue($i + 1, $value); } $this->Log->write('bound values', Log::LOG_LEVEL_USER); } catch (\PDOException $ex) { $this->Log->exception($ex); return false; } return true; }
/** * Generate PHP string for this table and field. * * @param array $array Row of results from constant list * @return bool|int * @uses Db::query() * @uses Db::quote() */ protected function generate($array = array()) { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_array_ne($array)) { $this->Log->write('array is invalid', Log::LOG_LEVEL_WARNING, Helpers::get_call_string()); return false; } // these fields need to be present in the array $fields = array('table_name', 'name_field', 'value_field', 'type'); $valid = true; // check for the existence of each field in the array and break if one of them does not exist foreach ($fields as $field) { if (!array_key_exists($field, $array)) { $valid = false; break; } } if (!$valid) { $this->Log->write('input invalid', Log::LOG_LEVEL_WARNING); return false; } // assign parameters to variables $table = $array['table_name']; $field = $array['name_field']; $value_field = $array['value_field']; $type = $array['type']; $prefix = array_key_exists('prefix', $array) ? $array['prefix'] : $table; // build SELECT query for field and value $sql = 'SELECT ' . $field . ', ' . $value_field . PHP_EOL; $sql .= ' FROM ' . $table . PHP_EOL; $this->Log->write('generate SQL', Log::LOG_LEVEL_USER, $sql); // get rows from table $rows = $this->query($sql, array(), 'iterator'); if (!$rows instanceof DbIterator) { $this->Log->write('could not find rows from query', Log::LOG_LEVEL_WARNING); return false; } $this->Log->write('found rows for generate query', Log::LOG_LEVEL_USER); // build PHP string with comments to indicate table and field used in generation $php = PHP_EOL . '/**' . PHP_EOL; $php .= ' * ' . $table . '.' . $field . PHP_EOL; $php .= ' */' . PHP_EOL; foreach ($rows as $row) { if ($row === null || !array_key_exists($field, $row)) { continue; } // prepare constant name (upper case, underscores instead of spaces, no multiple underscores together) $val = strtoupper(Helpers::space_to_underscore($prefix . '_' . $row[$field])); // add define statement to string $php .= 'define(\'' . $val . '\', ' . $this->quote($row[$value_field], $type) . ');' . PHP_EOL; } $php .= '// END ' . $table . '.' . $field . PHP_EOL . PHP_EOL; $this->Log->write('built PHP string with ' . strlen($php) . ' characters', Log::LOG_LEVEL_USER); if (!Helpers::is_string_ne($php)) { $this->Log->write('There was an issue building the PHP.', Log::LOG_LEVEL_WARNING, Helpers::get_type_size($php)); return false; } // append string to global string $this->php .= $php; return strlen($php); }