Yii2 UrlManager + wrong generate route - url

I have some problem - urlrules is correct but generated url from yii\bootstrap\Nav not correctly ->:
{domain}/armory/search?server=Lorem+ipsum
but this url working too -> {domain}/armory/search/Lorem+ipsum
'search/<server>' => 'search/index'
url rule ^
protected function addUrlManagerRules($app)
{
$app->urlManager->addRules([new GroupUrlRule([
'prefix' => $this->id,
'rules' => require __DIR__ . '/url-rules.php',
])],true);
}
why this not generate url like {domain}/armory/search/Lorem+ipsum
'url' => ['/armory/search', 'server' => 'Lorem+ipsum'],

You need to use pattern in the format <ParamName:RegExp>
Read here
try something like this in your urlManager section rules.
'site/search/<server:\w+>' => 'site/search'
Url::to(['site/search', 'server' => 'lorem_ipsum']) could be display site/search/lorem_ipsum

Related

How to create custom urls with dashes in Yii2?

I am working with advanced project application and trying to add URL rules in Yii2 to handle custom URLs with dashes.
What I want to do is to change the URL from
http://www.example.com/post/details?url=example-post-title
To
http://www.example.com/example-post-title
I have below configuration which works fine when the URL parameter does not have dash (exampleposttitle).
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// ...
'<url:\w+>' => 'post/details',
],
],
You need to fix your regexp, since \w+ does not allow dashes:
'<url:[\w-]+>' => 'post/details',

Requiring apt::source as a dependency gives a syntax error

I need to add the docker source list to apt before installing docker.
I have
apt::source { 'debian-jessie':
comment => 'This is the docker Debian jessie mirror',
location => 'http://apt.dockerproject.org/repo',
release => 'debian-jessie',
repos => 'main',
key_content => '58118E89F3A912897C070ADBF76221572C52609D',
key_server => 'keyserver.ubuntu.com',
ensure => present,
include_src => false,
include_deb => true,
}
which works, and also
package {'docker-engine':
ensure => present,
before => Class['docker'],
}
which works only after a second run (I use vagrant provision, but that's not relevant to the problem).
What I would like is making the whole thing work at the first provisioning by instructing puppet to execute apt::source before docker-engine, however adding it in require is not a valid syntax:
package {'docker-engine':
ensure => present,
before => Class['docker'],
require => [
Apt::source['debian-jessie'],
]
}
How to specify this dependency?
The rest of the file looks like this:
class { 'docker':
dns => '192.168.1.1',
manage_package => false,
use_upstream_package_source=> false,
# service_name => 'docker',
docker_command => 'docker',
package_name => 'docker-engine',
service_enable => true,
service_state => 'running',
extra_parameters => ["--insecure-registry=192.168.1.0/24"],
}
include 'docker'
file { "/lib/systemd/system/docker.service":
notify => Service["docker"],
ensure => present,
owner => "root",
group => "root",
mode => 0600,
source =>"puppet:///modules/docker/etc/systemd/system/docker.service"
} ~> Exec['systemctl-daemon-reload']
Capitalize word source
require => Apt::Source['debian-jessie']
Puppet documentation states:
The general form of a resource reference is:
The resource type, capitalized (every segment must be capitalized if
the resource type includes a namespace separator [::])
An opening square bracket
The title of the resource as a string, or a comma-separated list of titles
A closing square bracket

Writing general notes for API docs using swagger-docs

I am using swagger to document my Rails REST API using swagger-docs. I am able to document every method but the problem is that I want to write general guidelines() for using API. I cannot find any method that allows me to do so. Any help will be appreciated.
Something as shown in the image below:
You just need to set the "Description" attribute to some appropriate text when you initially configure Swagger.
Have a look at the first example from https://github.com/richhollis/swagger-docs:
Swagger::Docs::Config.register_apis({
"1.0" => {
# the extension used for the API
:api_extension_type => :json,
# the output location where your .json files are written to
:api_file_path => "public/api/v1/",
# the URL base path to your API
:base_path => "http://api.somedomain.com",
# if you want to delete all .json files at each generation
:clean_directory => false,
# add custom attributes to api-docs
:attributes => {
:info => {
"title" => "Swagger Sample App",
"description" => "This is a sample description.",
"termsOfServiceUrl" => "http://helloreverb.com/terms/",
"contact" => "apiteam#wordnik.com",
"license" => "Apache 2.0",
"licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
}
}
}
})
The example app at petstore.swagger.io shows you the general output and layout you'll get.

OAuth signature generation using HMAC-SHA1?

I apologize for asking so many questions but none of them seem to be getting answered and I really need help on this. I'm using LTI to integrate my program into a learning management system, and I need to authenticate using OAuth. I have no trouble generating a signature following the guidelines here but the signature I generate never matches the one passed to me by the LMS, and I can't figure out for the life of me why they never match. I'm hoping that it's something I'm oblivious to, but I really need some assistance on this.
When I launch my program from the LMS, I am sent this array via POST in what is called the LTI launch:
array(
'launch_presentation_locale' => 'EN-US__',
'tool_consumer_instance_guid' => 'key',
'tool_consumer_instance_name' => 'MyProgram',
'tool_consumer_instance_description' => 'MyProgram',
'tool_consumer_instance_contact_email' => 'johndoe#email.com',
'tool_consumer_info_version' => '10.3.0 SP5',
'tool_consumer_info_product_family_code' => 'desire2learn',
'context_id' => '2440554',
'context_title' => 'ContextTitle',
'context_label' => 'ContextTitle',
'context_type' => '',
'user_id' => 'USER_ID',
'roles' => 'None',
'lis_person_name_given' => 'John',
'lis_person_name_family' => 'Doe',
'lis_person_name_full' => 'John Doe',
'lis_person_contact_email_primary' => 'johndoe#email.com',
'ext_tc_profile_url' => 'https://profileurl.com',
'ext_d2l_token_id' => '123456789',
'ext_d2l_link_id' => '1234',
'ext_d2l_token_digest' => 'AbCdEfGhIjKlMnOpQrStUvWxYzi=',
'resource_link_id' => '',
'resource_link_title' => 'MyProgram',
'resource_link_description' => 'MyProgram',
'lis_result_sourcedid' => 'abcdefgh-ijkl-mnop-qrst-uvwxyz012345',
'lis_outcome_service_url' => 'https://outcomeserviceurl.com',
'lti_version' => 'LTI-1p0',
'lti_message_type' => 'basic-lti-launch-request',
'oauth_version' => '1.0',
'oauth_nonce' => '123456789',
'oauth_timestamp' => '1234567890',
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_consumer_key' => 'key',
'oauth_callback' => 'about:blank',
'oauth_signature' => 'eFUR8O5xVydLrj4PDj37nF4cq6A=',
'basiclti_submit' => 'Launch Endpoint with BasicLTI Data'
);
Here is what I'm trying. I've added comments to clarify the steps:
// Set variables that are required for the signature to be generated.
$OAUTH_KEY = 'key';
$OAUTH_SECRET = 'secret';
$httpMethod = 'POST';
$SITE_URL = 'https://localhost/test.php';
// make array copy of entire POST data, remove the 'oauth_signature' field as specified in the oauth spec from the copy array, then sort alphabetically. After that, url encode the key/value of each item in the copy array and store into a string for later use.
$request_parameter_array = $_POST;
unset($request_parameter_array['oauth_signature']);
ksort($request_parameter_array);
$request_parameter_str = '';
foreach($request_parameter_array as $key => $value) {
$request_parameter_str .= rawurlencode($key) . '=' . rawurlencode($value) . '&';
}
// create the signature base string (string variable that the actual signature is created from) by following these steps from the OAuth documentation:
// 1. The HTTP request method in uppercase. For example: "HEAD",
// "GET", "POST", etc. If the request uses a custom HTTP method, it
// MUST be encoded (Section 3.6).
// 2. An "&" character (ASCII code 38).
// 3. The base string URI from Section 3.4.1.2, after being encoded
// (Section 3.6).
// 4. An "&" character (ASCII code 38).
// 5. The request parameters as normalized in Section 3.4.1.3.2, after
// being encoded (Section 3.6).
$key = rawurlencode($OAUTH_SECRET) . '&';
$signature_base = strtoupper($httpMethod) . '&' . rawurlencode($SITE_URL) . '&';
$signature_base .= rawurlencode($request_parameter_str);
$signature = base64_encode(hash_hmac("sha1", $signature_base, $key, true));
echo $signature;
I guess my own stupidity was the issue, here. The issue was arising from D2L itself because I misunderstood what the difference was between using a tool link vs. a tool provider for my integrations. I literally deleted my tool provider and went with a tool link and now I'm able to authenticate every time.
Turns out there wasn't a problem with the code at all here.

Set mime_types not working for zip file in Symfony

In my Symfony application, I want to set zip file as one of the mime type during the file upload, the code to do this is below:
$this->validatorSchema ['Documents'] = new sfValidatorFile (
array ('mime_types' => array(
'application/zip',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
'application/x-zip',
'application/octet-stream',
'application/pdf') ),
array ('invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be of JPEG, PNG , GIF, pdf and zip format.' ) );
However, when I did the uploads, I found that all of pdf, png, gif etc can be uploaded. The only mime type that cannot be uploaded, are files that end with .zip.
What's going wrong here?
I asked a related, but different question here.
Note: I am using Symfony 1.2.7
Edit: I did some further testing with different browsers. Firefox 3 works because of 'application/octet-stream'', whereas IE works because of 'application/x-zip-compressed', Google Chrome doesn't work at all.
After more testing, I believe that this is a bug in Symfony.
Do an echo or error_log to see what $uploaded_file->getMime() [or whatever the right method call is] returns for your zip files.
If the string you get is one of the strings you pass to the array, there might be a bug with sfValidatorFile (I've never used it) and you might want to try using a yml validator.
this is a problem with mime type detection. the sfValidatorFile can use user supplied function to determine mime type. so you can write your own function to detect zip files from uploaded file if mime type that is determined by validator doesn't do the job right.
link to documentation, look at the end of page that explains file uploads.
There is a solution :
create your "sfValidatorFileZip.class.php" in your "/lib/Validator/". This class extend from the commun "sfValidatorFile" and it contain the "configure" methode as listed below :
class sfValidatorFileZip extends sfValidatorFile{
protected function configure($options = array(), $messages = array()){
if (!ini_get('file_uploads'))
{
throw new LogicException(sprintf('Unable to use a file validator as "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
}
$this->addOption('max_size');
$this->addOption('mime_types');
$this->addOption('mime_type_guessers', array(
array($this, 'guessFromFileinfo'),
array($this, 'guessFromMimeContentType'),
array($this, 'guessFromFileBinary'),
));
$this->addOption('mime_categories', array(
'web_images' => array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
),
'zip_file' => array(
'application/zip'
)
));
$this->addOption('validated_file_class', 'sfValidatedFile');
$this->addOption('path', null);
$this->addMessage('max_size', 'File is too large (maximum is %max_size% bytes).');
$this->addMessage('mime_types', 'Invalid mime type (%mime_type%).');
$this->addMessage('partial', 'The uploaded file was only partially uploaded.');
$this->addMessage('no_tmp_dir', 'Missing a temporary folder.');
$this->addMessage('cant_write', 'Failed to write file to disk.');
$this->addMessage('extension', 'File upload stopped by extension.');
}
}
in your form, the widget will be an instance from your custom "sfValidatorFileZip" an you have to precise that 'mime_types' => 'zip_file'. exemple :
$this->setValidator('filename', new sfValidatorFileZip(array(
'required' => false,
'mime_types' => 'zip_file',
'path' => sfConfig::get('sf_upload_dir') . '/files/',),
array('required' => 'filename is required.', 'mime_types' => 'only ZIP is accepted')));

Resources