Beispiel #1
0
 /**
  * Upload file
  * @param array $parameters The request parameters.
  * @param string $index The paramatern index for example 'file'.
  * @param array $move If $move is set (Is not (null or == array() or false ))
  * then the uploaded file will be moved to the specified directory.
  * The $move takes indexes 'path' and 'name', the path is the directory
  * the uploaded file will be moved. The name is optional and
  * @param array $allowedFiletypes *[Optional] Array with allowed extensions.
  * @param integer $max_size *[Optional] The maximum size of the
  * uploaded file in bytes. Default value is 10485760 bytes
  * @param boolean $parseExtension *[Optional] If true the destination
  * filename will be joined with files extension  Default value is false
  */
 public static function file($file, $move = [], $allowedFiletypes = ['csv'], $max_size = 10485760, $parseExtension = false)
 {
     if (!$file) {
         return 'Select a file';
     }
     $temporaryPath = $file['tmp_name'];
     if (!file_exists($temporaryPath)) {
         throw new NotFoundException('File not found');
     }
     $filename = $file['name'];
     $ext = Util::extension($filename);
     if (!in_array($ext, $allowedFiletypes)) {
         return 'Incorrect file type';
     }
     $size = filesize($temporaryPath);
     if ($size > $max_size) {
         return 'File size exceeds maximum';
     }
     if ($move) {
         if (!is_array($move)) {
             $move['path'] = $move;
         }
         if (isset($move['name']) && $parseExtension) {
             $move['name'] .= '.' . $ext;
         } elseif ($parseExtension) {
             $move['path'] .= '.' . $ext;
         }
         $destination = Util::get_path(isset($move['name']) ? [$move['path'], $move['name']] : [$move['path']]);
         if (!rename($temporaryPath, $destination)) {
             return 'Error uploading file';
         }
         return ['path' => $destination, 'name' => basename($destination), 'size' => filesize($destination), 'name_original' => basename($file['name'])];
     } else {
         return ['path' => $temporaryPath, 'name' => basename($temporaryPath), 'size' => filesize($temporaryPath), 'name_original' => basename($file['name'])];
     }
 }
Beispiel #2
0
 public function testQuery()
 {
     \Phramework\QueryLog\APP\Models\User::get();
     \Phramework\QueryLog\APP\Models\User::getById(1);
     \Phramework\QueryLog\APP\Models\User::post(\Phramework\Models\Util::readableRandomString(20));
 }
Beispiel #3
0
 /**
  * Prepare log object
  * @param  integer     $flags
  * @param  object      $settings
  * @param  object      $params
  * @param  string      $HTTPMethod
  * @param  array       $headers
  * @param  object|null $additionalParameters
  * @return object
  */
 private static function prepareObject($flags, $settings, $params, $HTTPMethod, $headers, $additionalParameters)
 {
     list($URI) = \Phramework\URIStrategy\URITemplate::URI();
     $object = (object) ['request_id' => Phramework::getRequestUUID(), 'URI' => $URI, 'method' => $HTTPMethod, 'user_id' => null, 'ip_address' => \Phramework\Models\Util::getIPAddress(), 'request_headers' => null, 'request_params' => null, 'request_body_raw' => null, 'request_timestamp' => $_SERVER['REQUEST_TIME'], 'response_timestamp' => time(), 'response_headers' => null, 'response_body' => null, 'response_status_code' => http_response_code(), 'exception' => null, 'exception_class' => null, 'errors' => null, 'call_trace' => null, 'flags' => $flags, 'additional_parameters' => $additionalParameters];
     if (($flags & self::LOG_USER_ID) !== 0) {
         $user = Phramework::getUser();
         $object->user_id = $user ? $user->id : false;
     }
     /*
        Request flags
     */
     if (($flags & self::LOG_REQUEST_HEADERS) !== 0) {
         //Asterisk authorization header value except schema
         if (isset($headers['Authorization'])) {
             list($authorizationSchema) = sscanf($headers['Authorization'], '%s %s');
             $headers['Authorization'] = $authorizationSchema . ' ***';
         }
         $object->request_headers = $headers;
     } else {
         $request_headers = [];
         if (($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) !== 0) {
             //Write content type
             $request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null;
         }
         if (($flags & self::LOG_REQUEST_HEADER_AGENT) !== 0) {
             $request_headers['User-Agent'] = isset($headers['User-Agent']) ? $headers['User-Agent'] : null;
         }
         if (($flags & self::LOG_REQUEST_HEADER_REFERER) !== 0) {
             $request_headers['Referer'] = isset($headers['Referer']) ? $headers['Referer'] : null;
         }
         if (($flags & self::LOG_REQUEST_HEADER_ACCEPT) !== 0) {
             $request_headers['Accept'] = isset($headers['Accept']) ? $headers['Accept'] : null;
         }
         if (!empty($request_headers)) {
             $object->request_headers = $request_headers;
         }
     }
     if (($flags & self::LOG_REQUEST_PARAMS) !== 0) {
         $object->request_params = $params;
     }
     if (($flags & self::LOG_REQUEST_BODY_RAW) !== 0) {
         $bodyRaw = file_get_contents('php://input');
         if (strlen($bodyRaw) > $settings->body_raw_limit) {
             $bodyRaw = 'TRIMMED' . PHP_EOL . substr($bodyRaw, 0, $settings->body_raw_limit);
         }
         //Apply FILTER_SANITIZE_STRING
         $object->request_body_raw = \Phramework\Models\Filter::string($bodyRaw);
         //include content type headers if disabled
         if (!empty($bodyRaw) && ($flags & self::LOG_REQUEST_HEADERS) === 0 && ($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) === 0) {
             $contentType = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null;
             if (empty($object->request_headers)) {
                 //make sure it's array
                 $object->request_headers = [];
             }
             $object->request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = $contentType;
         }
     }
     $responseHeaders = new \stdClass();
     foreach (headers_list() as $header) {
         list($key, $value) = explode(': ', $header);
         $responseHeaders->{$key} = $value;
     }
     /*
        Response flags
     */
     if (($flags & self::LOG_RESPONSE_HEADER) !== 0) {
         $object->response_headers = $responseHeaders;
     }
     if (($flags & self::LOG_RESPONSE_BODY) !== 0) {
         $object->response_body = ob_get_contents();
         if (($flags & self::LOG_RESPONSE_HEADER) === 0) {
             //show content type if headers are disabled
             $object->response_headers = (object) [\Phramework\Models\Request::HEADER_CONTENT_TYPE => isset($responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE}) ? $responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE} : null];
         }
     }
     return $object;
 }
Beispiel #4
0
 /**
  * @throws \Exception
  */
 private static function decompressTar($compressedFile, $destinationFolder, $originalFilename = null, $allowedExtensions = [])
 {
     try {
         $zip = new \PharData($compressedFile);
     } catch (\Exception $e) {
         throw new \Exception('Cannot open tar archive');
     }
     $files = [];
     foreach ($zip as $file) {
         $name = $file->getFileName();
         if (in_array(Util::extension($name), $allowedExtensions)) {
             $files[] = $name;
         }
     }
     if (!$files) {
         throw new \Exception('No valid files found inside archive');
     }
     $zip->extractTo($destinationFolder, $files);
     return $files;
 }
Beispiel #5
0
 /**
  * Initialize API
  *
  * Only one instance of API may be present
  * @param array $settings
  * @param Phramework\URIStrategy\IURIStrategy $URIStrategy
  * URIStrategy object
  * @param object|null $translationObject  *[Optional]* Set custom translation class
  * @throws Phramework\Exceptions\ServerException
  */
 public function __construct($settings, $URIStrategyObject, $translationObject = null)
 {
     self::$settings = $settings;
     self::$user = false;
     self::$language = 'en';
     self::$requestUUID = \Phramework\Models\Util::generateUUID();
     //Instantiate StepCallback object
     self::$stepCallback = new \Phramework\Extensions\StepCallback();
     if (!is_subclass_of($URIStrategyObject, \Phramework\URIStrategy\IURIStrategy::class, true)) {
         throw new \Phramework\Exceptions\ServerException('Class is not implementing Phramework\\URIStrategy\\IURIStrategy');
     }
     self::$URIStrategy = $URIStrategyObject;
     //If custom translation object is set add it
     if ($translationObject) {
         self::setTranslation($translationObject);
     } else {
         //Or instantiate default translation object
         //sef::$translation = new \Phramework\Extensions\Translation(
         //    self::getSetting('language'),
         //    self::getSetting('translation', 'track_missing_keys', null, false)
         //);
     }
     self::$instance = $this;
 }
Beispiel #6
0
 /**
  * Delete all contents from a directory
  * @param string $directory Directory path
  * @param boolean $DELETE_DIRECTORY *[Optional]*, if is set directory will be deleted too.
  */
 public static function deleteDirectoryContents($directory, $DELETE_DIRECTORY = false)
 {
     $files = array_diff(scandir($directory), ['.', '..']);
     foreach ($files as $file) {
         $path = Util::get_path([$directory, $file]);
         is_dir($path) ? self::delete_directory_contents($path, true) : unlink($path);
     }
     return $DELETE_DIRECTORY ? rmdir($directory) : true;
 }