/** * Hash a password with a given salt. * * @param string $password * @param string $salt * @return string * @uses is_string_ne() from helpers.php */ public static function hash($password = '', $salt = '') { // input validation if (!Helpers::is_string_ne($password) || !Helpers::is_string_ne($salt)) { return ''; } return md5(md5($salt) . 'mts7' . md5($password)); }
/** * Write contents to a file. * * @param string $file * @param string $content * @return bool|int */ private function writeFile($file = '', $content = '') { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); if (!Helpers::is_string_ne($file) || !is_file($file) || !file_exists($file)) { $this->Log->write('file is not a string or does not exist for writing', Log::LOG_LEVEL_WARNING); return false; } return file_put_contents($file, $content . PHP_EOL, FILE_APPEND); }
/** * Save the password in the database. * * @param string $password * @return bool|mixed */ private function savePassword($password = '') { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_string_ne($password)) { $this->Log->write('password must be provided', Log::LOG_LEVEL_WARNING); return false; } $pairs = array('password' => $password); $updated = $this->update($this->table, $pairs, array($this->id_field => $this->id)); $this->Log->write('password saved', Log::LOG_LEVEL_USER, Helpers::get_string($updated)); return $updated; }
/** * Get file from input and move it to the output directory. * * @return bool */ public function processAjax() { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!$this->ajax) { $this->Log->write('AJAX must be specified to do AJAX uploads', Log::LOG_LEVEL_WARNING); return false; } if (!Helpers::is_string_ne($this->upload_file_name)) { $this->Log->write('a file name must be specified', Log::LOG_LEVEL_WARNING); return false; } // set post_file $this->post_file['tmp_name'] = $this->upload_file_name; $this->post_file['name'] = $this->upload_file_name; $this->Log->write('set name fields in post_file', Log::LOG_LEVEL_USER, $this->post_file); $file = file_get_contents('php://input'); $this->post_file['size'] = strlen($file); if (!$this->validateFile()) { $this->Log->write('file is invalid', Log::LOG_LEVEL_WARNING); return false; } // set output path $this->outputPath($this->output_path); $this->output_file = $this->uniqueName(); $this->Log->write('output_file', Log::LOG_LEVEL_USER, $this->output_file); // write file to output_file $bytes = file_put_contents($this->output_path . $this->output_file, $file); $this->Log->write('wrote ' . $bytes . ' bytes', Log::LOG_LEVEL_USER, $this->output_path . $this->output_file); return $bytes > 0; }
/** * Validate United States phone number. * * @param string $phone * @param bool $strict * @return bool */ public function phone($phone = '', $strict = false) { if (!Helpers::is_string_ne($phone)) { $this->error_message = 'Empty input: phone'; return false; } // phone should be 10 digits if ($strict === true) { $valid = strlen($phone) === 10; if (!$valid) { $this->error_message = 'Phone is not 10 digits'; } } else { // strip all non-numeric values $phone = preg_replace('[\\D]', '', $phone); $valid = strlen($phone) === 10; if (!$valid) { $this->error_message = 'Stripped phone is not 10 digits'; } } // verify this is not a 555 number if it is already valid if ($valid) { $valid = substr($phone, 3, 3) !== '555'; if (!$valid) { $this->error_message = 'Phone contains xxx555xxxx'; } } return $valid; }
/** * 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; }
/** * 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); }
/** * Magic method setter * * @param string $name * @param $value * @return bool */ public function __set($name = '', $value) { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); if (!Helpers::is_string_ne($name)) { $this->Log->write('name not set', Log::LOG_LEVEL_WARNING); return false; } $method = Helpers::upper_camel($name); if (method_exists($this, $method)) { $this->Log->write('method ' . $method . ' exists', Log::LOG_LEVEL_USER); $this->{$method}($value); } else { $property = Helpers::lower_underscore($name); if (!property_exists($this, $property)) { $this->Log->write($property . ' does not exist as a property', Log::LOG_LEVEL_WARNING); return false; } $type_value = gettype($value); $type_property = gettype($this->{$property}); if ($type_value === $type_property) { $this->Log->write('types match for ' . $property, Log::LOG_LEVEL_USER, $type_property); $this->{$property} = $value; } else { $this->Log->write($type_value . ' != ' . $type_property . '; consider type casting', Log::LOG_LEVEL_WARNING); return false; } } return true; }
/** * Replace the extension of $input_file with the one provided as a parameter. * * @param string $ext * @return bool|mixed * @uses Convert::$input_file */ private function replaceExtension($ext = '') { $this->Log->write('Convert::replaceExtension()', Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_string_ne($ext)) { $this->Log->write('Extension must be provided.', Log::LOG_LEVEL_WARNING); return false; } $current = pathinfo($this->input_file, PATHINFO_EXTENSION); return preg_replace('/' . $current . '$/', $ext, $this->input_file); }