### Manual Installation Configuration for CodeIgniter Signed URL Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/installation.md Manually install the CodeIgniter Signed URL library by downloading the project, placing it in the 'app/ThirdParty/signed-url' directory, and configuring the 'app/Config/Autoload.php' file. This involves adding the library's namespace to the `$psr4` array and including 'Common.php' in the `$files` array. ```php APPPATH, 'Config' => APPPATH . 'Config', 'Michalsn\CodeIgniterSignedUrl' => APPPATH . 'ThirdParty/signed-url/src', ]; ... public $files = [ APPPATH . 'ThirdParty/signed-url/src/Common.php', ]; ``` -------------------------------- ### Install CodeIgniter Signed URL via Composer Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/installation.md This command installs the CodeIgniter Signed URL library using Composer. Ensure Composer is installed and accessible in your project's root directory. ```console composer require michalsn/codeigniter-signed-url ``` -------------------------------- ### Generate Encryption Key for CodeIgniter Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/installation.md Generates the encryption key required for CodeIgniter applications, including those using the Signed URL library. This command should be run from the project's root directory using the Spark CLI. ```console php spark key:generate ``` -------------------------------- ### Publish Signed URL Configuration Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/configuration.md This command publishes the configuration file for the Signed URL package, allowing for easy modification of its settings. It is a one-time setup step. ```bash php spark signedurl:publish ``` -------------------------------- ### Install and Publish CodeIgniter Signed URL Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Installs the CodeIgniter Signed URL package using Composer, generates a necessary encryption key, and optionally publishes the configuration file for customization. ```bash composer require michalsn/codeigniter-signed-url php spark key:generate php spark signedurl:publish ``` -------------------------------- ### Generate and Verify Signed URLs in PHP Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Provides a comprehensive example of using the CodeIgniter Signed URL library. It covers generating signed URLs for file downloads with expiration, creating password reset links, configuring the `signedurl` filter for automatic route protection, and performing manual URL verification. ```php 'Report Q4.pdf', 'url' => signedurl() ->setExpiration(HOUR * 24) ->siteUrl('downloads/file/report-q4.pdf') ], [ 'name' => 'Presentation.pptx', 'url' => signedurl() ->setExpiration(HOUR * 24) ->siteUrl('downloads/file/presentation.pptx') ], ]; // Generate password reset link (expires in 1 hour) $resetUrl = signedurl() ->setExpiration(HOUR) ->siteUrl('auth/reset-password?user=' . $userId . '&token=' . $resetToken); return view('dashboard', compact('files', 'resetUrl')); } } // ===== FILTER-PROTECTED ROUTE (automatic verification) ===== // app/Config/Filters.php public array $filters = [ 'signedurl' => ['before' => ['downloads/*']], ]; // Controller - no manual verification needed, filter handles it class DownloadController extends BaseController { public function file($filename) { // If we reach here, the URL signature is valid $path = WRITEPATH . 'uploads/' . $filename; if (!is_file($path)) { throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound(); } return $this->response->download($path, null); } } // ===== MANUAL VERIFICATION (without filter) ===== class VerifyController extends BaseController { public function email() { try { signedurl()->verify($this->request); $userId = $this->request->getGet('user'); // Mark email as verified... return redirect()->to('/login')->with('message', 'Email verified!'); } catch (SignedUrlException $e) { return redirect()->to('/')->with('error', 'Invalid or expired verification link.'); } } } ``` -------------------------------- ### Register SignedUrl Service in CodeIgniter Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt This PHP code demonstrates how to access the SignedUrl library through CodeIgniter's service container. You can retrieve the shared instance using `service('signedurl')`, get a new instance using `ConfigServices::signedurl(null, false)`, or provide a custom configuration object to `ConfigServices::signedurl()` for specific settings like expiration or algorithm. ```php siteUrl('controller/method'); // Getting a new instance (not shared) $signedUrl = \Config\Services::signedurl(null, false); // With custom configuration $config = new \Michalsn\CodeIgniterSignedUrl\Config\SignedUrl(); $config->expiration = HOUR; $config->algorithm = 'sha512'; $signedUrl = \Config\Services::signedurl($config, false); ``` -------------------------------- ### List Available Signing Algorithms Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/configuration.md This command displays a list of all supported algorithms that can be used for signing URLs. It's useful for understanding the available security options. ```bash php spark signedurl:algorithms ``` -------------------------------- ### CLI Commands Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Use Spark commands to manage the library configuration and view available options. ```APIDOC ## CLI Commands ### Description Use Spark commands to manage the library configuration and view available options. ### Method Run commands using `php spark`. ### Endpoint N/A ### Parameters None ### Request Example ```bash # Publish configuration file to app/Config/SignedUrl.php php spark signedurl:publish # List all available hashing algorithms php spark signedurl:algorithms # Output: # md5 # sha1 # sha256 # sha384 # sha512 # ... (many more) ``` ### Response N/A (These are CLI commands, not API responses). ### Response Example N/A ``` -------------------------------- ### CodeIgniter SignedUrl CLI Commands Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt This section outlines the command-line interface (CLI) commands available for the CodeIgniter Signed URL library using the `spark` tool. The `signedurl:publish` command copies the configuration file to `app/Config/SignedUrl.php`, while `signedurl:algorithms` lists all supported hashing algorithms for use in the configuration. ```bash # Publish configuration file to app/Config/SignedUrl.php php spark signedurl:publish # List all available hashing algorithms php spark signedurl:algorithms # Output: # md5 # sha1 # sha256 # sha384 # sha512 # ... (many more) ``` -------------------------------- ### Handle Signed URL Exceptions in PHP Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Demonstrates how to catch and handle `SignedUrlException` which can be thrown during URL verification. This includes logging the error and returning an appropriate HTTP response. ```php verify($this->request); } catch (SignedUrlException $e) { // Possible error messages: // - "This URL have to be signed." (missing signature) // - "Algorithm is invalid or not supported." (tampered algorithm) // - "URL is not valid." (signature mismatch - URL was modified) // - "This URL has expired." (past expiration timestamp) log_message('warning', 'Signed URL verification failed: ' . $e->getMessage()); return $this->response ->setStatusCode(403) ->setJSON(['error' => $e->getMessage()]); } ``` -------------------------------- ### Configuration Options Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Customize the library behavior by publishing and editing the configuration file. ```APIDOC ## Configuration Options ### Description Customize the library behavior by publishing and editing the configuration file (`app/Config/SignedUrl.php`). ### Method Edit the `app/Config/SignedUrl.php` file. ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php sign()` method. It shows both direct signing and signing with a specified expiration time using the `setExpiration()` method, which takes a duration like `HOUR`. The resulting signed URL includes `signature` and `expires` query parameters. ```php sign($uri); // Output: https://example.com/path?query=string&signature=... // With expiration $uri = new URI('https://example.com/api/resource/123'); $signedUrl = signedurl()->setExpiration(HOUR)->sign($uri); // Output: https://example.com/api/resource/123?expires=...&signature=... ``` -------------------------------- ### Customize SignedUrl Configuration in PHP Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt This PHP configuration file (`app/Config/SignedUrl.php`) allows customization of the Signed URL library's behavior. You can set default expiration times, token lengths, hashing algorithms, and query parameter names for expiration, token, signature, and algorithm. Options for filter error handling, such as redirection and showing 404 pages, are also available. ```php verify($this->request); ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php verify($this->request); // URL is valid - proceed with download return $this->response->download('file.pdf', null); } catch (SignedUrlException $e) { // Handle different error cases if ($e->getMessage() === 'This URL have to be signed.') { return redirect()->to('/')->with('error', 'Invalid access link'); } if ($e->getMessage() === 'This URL has expired.') { return redirect()->to('/')->with('error', 'This link has expired'); } if ($e->getMessage() === 'URL is not valid.') { return redirect()->to('/')->with('error', 'Link has been tampered with'); } throw $e; } } } ``` ### Response #### Success Response (200) Returns `true` if the URL is valid and not expired. #### Response Example ```json { "success": true } ``` #### Error Response Throws `SignedUrlException` with specific messages for invalid signature, expiration, or tampering. ``` -------------------------------- ### sign() Method Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Low-level method that transforms a URI object into a signed URL. Usually called internally by `siteUrl()` and `urlTo()`. ```APIDOC ## sign() Method ### Description Low-level method that transforms a URI object into a signed URL. Usually called internally by `siteUrl()` and `urlTo()`. ### Method ```php signedurl()->sign($uri); ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php sign($uri); // Output: https://example.com/path?query=string&signature=... // With expiration $uri = new URI('https://example.com/api/resource/123'); $signedUrl = signedurl()->setExpiration(HOUR)->sign($uri); // Output: https://example.com/api/resource/123?expires=...&signature=... ``` ### Response #### Success Response (200) Returns the signed URL as a string. #### Response Example ```json { "signed_url": "https://example.com/path?query=string&signature=..." } ``` ``` -------------------------------- ### Generate Signed URLs with siteUrl() Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Generates signed URLs from relative paths using the `siteUrl()` method, mirroring CodeIgniter's native helper. Supports query strings, array-based paths, and setting expiration times. ```php siteUrl('controller/method?query=string'); // Output: https://example.com/controller/method?query=string&signature=abc123... // Chain methods for expiration $url = signedurl()->setExpiration(DAY * 2)->siteUrl('download/file/123'); // Output: https://example.com/download/file/123?expires=1671980371&signature=xyz789... // Simple URL with query string $url = signedurl()->siteUrl('controller/method?query=string'); // Output: https://example.com/controller/method?query=string&signature=ongZW4ttfJMqN757mwNXp5kx_3snwQhaDyI6JiV-5FM // Using array syntax for path segments $url = signedurl()->siteUrl(['api', 'users', '42']); // Output: https://example.com/api/users/42?signature=... // With expiration (2 days) $url = signedurl()->setExpiration(DAY * 2)->siteUrl('downloads/premium-file'); // Output: https://example.com/downloads/premium-file?expires=1671980371&signature=IzHjHhkTOOBPTayZnk8f_ut0H4-3q0YrDb11slKPWWE // Multiple parameters $url = signedurl()->setExpiration(HOUR)->siteUrl('verify/email?user=123&token=abc'); // Output: https://example.com/verify/email?user=123&token=abc&expires=...&signature=... ``` -------------------------------- ### Generate Signed URL with Expiration Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/helpers.md The signedurl() function returns an instance of the SignedUrl class, allowing you to set an expiration time and specify a site URL for generating signed URLs. This is useful for time-limited access to specific controller methods. ```php signedurl()->setExpiration(DAY)->siteUrl('controller/method'); ``` -------------------------------- ### Sign a URI Directly (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/methods.md Directly signs a given URI. This method is typically used internally by other signing methods like siteUrl() and urlTo() and is not usually called directly by the user. ```php service('signedurl')->sign($uri); ``` -------------------------------- ### Generate Signed URLs with urlTo() Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Generates signed URLs using named routes, similar to CodeIgniter's `url_to()` helper. Accepts a route name and any required parameters, with support for setting expiration times. ```php get('user/(:num)', 'UserController::show/$1', ['as' => 'user.show']); // $routes->get('download/(:segment)/(:num)', 'DownloadController::file/$1/$2', ['as' => 'download.file']); // Generate signed URL for named route $url = signedurl()->urlTo('user.show', 42); // Output: https://example.com/user/42?signature=... // With multiple parameters $url = signedurl()->urlTo('download.file', 'documents', 789); // Output: https://example.com/download/documents/789?signature=... // With expiration $url = signedurl()->setExpiration(WEEK)->urlTo('user.show', 42); // Output: https://example.com/user/42?expires=...&signature=... ``` -------------------------------- ### Verify Signed URL in CodeIgniter Controller Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt This PHP snippet demonstrates how to verify a signed URL within a CodeIgniter controller. It uses a try-catch block to handle potential `SignedUrlException` errors, providing specific user feedback for different validation failures like invalid signatures or expired links. The `signedurl()->verify($this->request)` method is central to this process. ```php verify($this->request); // URL is valid - proceed with download return $this->response->download('file.pdf', null); } catch (SignedUrlException $e) { // Handle different error cases if ($e->getMessage() === 'This URL have to be signed.') { return redirect()->to('/')->with('error', 'Invalid access link'); } if ($e->getMessage() === 'This URL has expired.') { return redirect()->to('/')->with('error', 'This link has expired'); } if ($e->getMessage() === 'URL is not valid.') { return redirect()->to('/')->with('error', 'Link has been tampered with'); } throw $e; } } } ``` -------------------------------- ### Service Registration Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Access the SignedUrl class through CodeIgniter's service container for dependency injection. ```APIDOC ## Service Registration ### Description Access the SignedUrl class through CodeIgniter's service container for dependency injection. ### Method Use the `service()` function or `Config\Services` class. ### Endpoint N/A ### Parameters None ### Request Example ```php siteUrl('controller/method'); // Getting a new instance (not shared) $signedUrl = \Config\Services::signedurl(null, false); // With custom configuration $config = new \Michalsn\CodeIgniterSignedUrl\Config\SignedUrl(); $config->expiration = HOUR; $config->algorithm = 'sha512'; $signedUrl = \Config\Services::signedurl($config, false); ``` ### Response N/A (This section describes service access, not an API response). ### Response Example N/A ``` -------------------------------- ### Generate Signed Site URL (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/methods.md Creates a signed URL similar to CodeIgniter's standard site_url function. This method ensures the generated URL includes a signature for verification purposes. ```php service('signedurl')->siteUrl('controller/method'); ``` -------------------------------- ### Generate Signed URL for Named Route (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/methods.md Generates a signed URL for a named route, similar to CodeIgniter's url_to function. This method allows for parameter inclusion and ensures the URL is signed. ```php service('signedurl')->urlTo('namedRoute', 'param'); ``` -------------------------------- ### Set Expiration for Signed URLs Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt Sets a temporary expiration time in seconds for the next generated signed URL. The expiration value resets after each URL generation and can be set to `null` to disable expiration for that specific call. ```php setExpiration(SECOND * 10)->siteUrl('path'); // Expire in 1 hour $url = signedurl()->setExpiration(HOUR)->siteUrl('temporary-access'); // Expire in 2 days $url = signedurl()->setExpiration(DAY * 2)->siteUrl('download/file'); // Expire in 1 week $url = signedurl()->setExpiration(WEEK)->urlTo('namedRoute', 123); // Disable expiration for this call (even if config has default expiration) $url = signedurl()->setExpiration(null)->siteUrl('permanent-link'); ``` -------------------------------- ### Verify Signed URL (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/methods.md Verifies if a given URL is properly signed and has not expired. This method can be used independently or in conjunction with the Filter class for automatic verification. ```php service('signedurl')->verify($request); ``` -------------------------------- ### SignedUrl Filter Source: https://context7.com/michalsn/codeigniter-signed-url/llms.txt The built-in filter automatically validates signed URLs before requests reach controllers. Configure it in `app/Config/Filters.php`. ```APIDOC ## SignedUrl Filter ### Description The built-in filter automatically validates signed URLs before requests reach controllers. Configure it in `app/Config/Filters.php`. ### Method Applied in `app/Config/Filters.php` using the `signedurl` alias. ### Endpoint Configured for specific routes in `app/Config/Filters.php`. ### Parameters None (configuration is done in `app/Config/Filters.php` and `app/Config/SignedUrl.php`). ### Request Example ```php \Michalsn\CodeIgniterSignedUrl\Filters\SignedUrl::class, ]; // Apply filter to specific routes public array $filters = [ 'signedurl' => [ 'before' => [ 'downloads/*', // All download routes 'verify/*', // Email verification routes 'api/secure/*', // Secure API endpoints 'premium-content/*', // Premium content access ] ], ]; } ``` ### Response #### Success Response (200) If the URL is valid, the request proceeds to the controller. #### Response Example No direct response, the filter allows the request to pass through. #### Error Response If the URL is invalid, the filter will redirect or show a 404 page based on the configuration in `app/Config/SignedUrl.php`. ``` -------------------------------- ### Generate Site URL with Signature (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/README.md Generates a signed URL for a given path, similar to CodeIgniter's site_url() function. This method appends a signature to the URL to prevent manipulation. It relies on the 'signedurl' helper being available. ```php echo signedurl()->siteUrl('controller/method?query=string'); // https://example.com/controller/method?query=string&signature=signature-goes-here ``` -------------------------------- ### Set Expiration for Signed URL (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/methods.md Sets a temporary expiration time for a signed URL. The expiration is specified in seconds and added to the current Unix timestamp. This value is reset when siteUrl(), urlTo(), or sign() methods are called. Configuration file settings can override this. ```php service('signedurl')->setExpiration(DAY)->siteUrl('url'); ``` -------------------------------- ### Configure Signed URL Filter in CodeIgniter Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/docs/filters.md This snippet shows how to define the signed URL filter in the CodeIgniter configuration file. It specifies that the 'signedurl' filter should be applied before requests matching the 'signed-urls/*' route pattern. ```php // app/Config/Filters.php ['before' => ['signed-urls/*']], ]; } ``` -------------------------------- ### Generate Named Route URL with Expiration and Signature (PHP) Source: https://github.com/michalsn/codeigniter-signed-url/blob/develop/README.md Generates a signed URL for a named route with a specified expiration time. This method allows for time-limited access to routes. It requires the 'signedurl' helper and defines constants like DAY for expiration periods. ```php echo signedurl()->setExpiration(DAY * 2)->urlTo('namedRoute', 12); // https://example.com/route/name/12?expiration=1671980371&signature=signature-goes-here ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.