/** * Login constructor. * * @param array $params */ public function __construct($params = array()) { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); if (Helpers::is_array_ne($params)) { if (array_key_exists('log_level', $params)) { $this->logLevel($params['log_level']); } $this->Log->write('params', Log::LOG_LEVEL_USER, $params); // set properties based on parameters using magic method foreach ($params as $name => $value) { $this->__set($name, $value); } } parent::__construct($params); }
/** * Replace bound parameter placeholders with parameters and write query to a file. * WARNING: This method uses more memory and time. Only use it if necessary and NOT on production. * * @param string $sql * @param array $params * @return string * @see http://php.net/manual/en/pdostatement.debugdumpparams.php#113400 */ private function writeQueryParameters($sql = '', $params = array()) { // @todo: ONLY ENABLE THIS METHOD IF NEEDED FOR DEBUGGING if (true) { return true; } $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); if (Helpers::is_array_ne($params)) { foreach ($params as $v) { $v = str_replace('?', '~', $this->quote($v)); $sql = preg_replace('/\\?/', $v, $sql, 1); } // free some memory unset($params); } // check for comment if (substr($sql, 0, 2) === '/*' && substr($sql, -2) === '*/') { // this is a multi-line comment $mult = strstr($sql, 'SQLSTATE') ? 4 : 2; $query = $sql . str_repeat(PHP_EOL, $mult); } else { $query = rtrim($sql, ';' . PHP_EOL) . ';' . PHP_EOL . PHP_EOL; } // free some memory unset($sql); return file_put_contents(ASSETS_DIR . 'sql/queries_' . date('Y-m-d') . '.sql', $query, FILE_APPEND); }
/** * Validate path, size, extension with post_file. * * @return bool */ private function validateFile() { // make sure there is a post file if (!Helpers::is_array_ne($this->post_file)) { $this->Log->write('The $_FILES array has not been saved. Please call init() first.', Log::LOG_LEVEL_WARNING); return false; } // verify output directory exists if (!is_dir($this->output_path)) { $this->Log->write('Output path not specified or does not exist. Please set the output path with Upload::outputPath() and process again.', Log::LOG_LEVEL_WARNING); return false; } // check max file size if (!$this->validateMaxSize()) { $this->Log->write('File size {' . $this->humanSize($this->post_file['size']) . '} is greater than max file size {' . $this->humanSize($this->max_size) . '}.', Log::LOG_LEVEL_WARNING); return false; } // validate extension $ext = pathinfo($this->post_file['name'], PATHINFO_EXTENSION); if (!in_array($ext, $this->accepted_exts)) { $this->Log->write('extension {' . $ext . '} is not allowed. Please upload a file with one of these extensions', Log::LOG_LEVEL_WARNING, $this->accepted_exts); return false; } return true; }
/** * Get the user from the database and store in a property. * * @return array|bool|mixed */ private function getUser() { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // use cached version if (Helpers::is_array_ne($this->user_data)) { return $this->user_data; } // determine which field and value to use in the query if (Helpers::is_valid_int($this->id, true)) { $where_field = $this->id_field; $value = $this->id; } elseif (Helpers::is_string_ne($this->name)) { $where_field = $this->name_field; $value = $this->name; } elseif (array_key_exists('user_id', $_SESSION)) { $where_field = $this->id_field; $value = $_SESSION['user_id']; } elseif (array_key_exists('user_name', $_SESSION)) { $where_field = $this->name_field; $value = $_SESSION['user_name']; } else { $this->Log->write('cannot determine user data to use', Log::LOG_LEVEL_WARNING); return false; } // build query $sql = 'SELECT *' . PHP_EOL; $sql .= ' FROM ' . $this->table . PHP_EOL; $sql .= ' WHERE ' . $where_field . ' = ?'; $row = $this->query($sql, array($value), 'first'); if (!Helpers::is_array_ne($row)) { return false; } $this->user_data = $row; $this->id = $row['id']; $this->name = $row['name']; return $row; }
/** * Log constructor. * * @param array $params */ public function __construct($params = array()) { // set defaults $this->log_level = $this::LOG_LEVEL_ERROR; if (defined('LOG_DIR')) { $this->logDirectory(LOG_DIR); } // set values from parameters if (Helpers::is_array_ne($params)) { if (array_key_exists('log_directory', $params)) { $this->logDirectory($params['log_directory']); } if (array_key_exists('log_level', $params) && Helpers::is_valid_int($params['log_level'])) { $this->logLevel($params['log_level']); } if (array_key_exists('file', $params) && Helpers::is_string_ne($params['file'])) { $this->file($params['file']); } if (array_key_exists('date_format', $params) && Helpers::is_string_ne($params['date_format'])) { $this->date_format = $params['date_format']; } if (array_key_exists('separator', $params) && Helpers::is_string_ne($params['separator'])) { $this->separator = $params['separator']; } } }
/** * Insert values into to table. * * @param string $table * @return bool|string * @uses DatabaseMap::$to_values * @uses Db::buildInsert() * @uses Db::begin() * @uses Db::query() * @uses Db::commit() */ private function insertValues($table = '') { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_string_ne($table) || !Helpers::is_array_ne($this->to_values[$table])) { $this->Log->write('table is invalid or to values does not contain table ' . $table, Log::LOG_LEVEL_WARNING); return false; } // get values for table $values = $this->to_values[$table]; // build insert query and parameters $result = $this->buildInsert($table, $values, true, true); if ($result === false) { $this->Log->write('could not build insert for ' . $table, Log::LOG_LEVEL_WARNING); return false; } list($sql, $parameters) = $result; $this->Log->write('trying insert query in transaction', Log::LOG_LEVEL_USER); $this->begin(); $this->query($sql, $parameters, 'insert'); if ($this->debug) { $this->Log->write('rolling back transaction due to debug', Log::LOG_LEVEL_USER); $this->rollback(); $output = $sql . PHP_EOL . Helpers::get_string($parameters); } else { $this->Log->write('committing transaction', Log::LOG_LEVEL_USER); $this->commit(); $output = true; } return $output; }
/** * Set and/or get user password for PDF file. If this is set, the PDF file will be encrypted. * * @return string */ public function userPassword() { $args = func_get_args(); if (Helpers::is_array_ne($args)) { if (Helpers::is_string_ne($args[0])) { $this->user_password = $args[0]; } else { $this->Log->write('invalid type for user password', Log::LOG_LEVEL_WARNING); } } return $this->user_password; }
/** * 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); }
/** * Upload a file to convert and pass the output name to inputFile. * * @param string $form_file * @param string $expected_type * @return bool|mixed|string * @uses Convert::$extensions * @uses Upload::uniqueName() * @uses Upload::process() * @uses Upload::lastMessage() * @uses Upload::outputPath() * @uses Convert::inputFile() * @see Log::last() */ public function uploadFile($form_file = 'upload_file', $expected_type = 'text') { $this->Log->write('Convert::uploadFile()', Log::LOG_LEVEL_SYSTEM_INFORMATION); if (!Helpers::is_string_ne($form_file)) { $this->Log->write('Form file name must be provided.', Log::LOG_LEVEL_WARNING); return false; } $exts = $this->extensions[$expected_type]; if (!Helpers::is_array_ne($exts)) { $this->Log->write('Expected type {' . $expected_type . '} is not valid.', Log::LOG_LEVEL_WARNING); return false; } $message = ''; $target_dir = '~/uploads/'; $Upload = new Upload(['form_file' => 'upload_file', 'output_file' => $target_dir, 'exts' => $exts]); $name = $Upload->uniqueName(); if ($name !== false) { $processed = $Upload->process(); if (!$processed) { $message = $Upload->lastMessage(); } else { $this->inputFile($Upload->outputPath() . $name); } } else { $message = $Upload->lastMessage(); } return $message; }