Skip to content

Commit

Permalink
Use RFC3986 URL encoding and fix mailto: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman committed Mar 24, 2016
1 parent df316d3 commit 6fd896e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ Add Blade templating code in *share.mynewservice* view file to generate a URL fo

Example:

https://mynewservice.example.com?url={{ urlencode($url) }}<?php echo $sep; ?>title={{ urlencode("Check this out! $title. See it here: $url") }}
https://mynewservice.example.com?url={{ rawurlencode($url) }}<?php echo $sep; ?>title={{ rawurlencode("Check this out! $title. See it here: $url") }}

Another example for the *email* service. Change the service config to be *[ 'view' => 'whatever' ]* and put this in the view file:

mailto?subject={{ urlencode("Wow, check this: $title") }}<?php echo $sep; ?>body={{ urlencode("Check this out! $title. See it here: $url") }}
mailto:?subject={{ rawurlencode("Wow, check this: $title") }}<?php echo $sep; ?>body={{ rawurlencode("Check this out! $title. See it here: $url") }}

Localizing? Easy, use Laravel's trans() call:

mailto:subject={{ urlencode(trans('share.email-subject', compact('url', 'title', 'media'))) }}<?php echo $sep ?>body={{ urlencode(trans('share.email-body', compact('url', 'title', 'media'))) }}
mailto:?subject={{ rawurlencode(trans('share.email-subject', compact('url', 'title', 'media'))) }}<?php echo $sep ?>body={{ rawurlencode(trans('share.email-body', compact('url', 'title', 'media'))) }}

Create a file at resources/lang/en/share.php with your choice of subject and body. URLs arguably have a maximum length of 2000 characters.

Expand Down
2 changes: 1 addition & 1 deletion src/views/default.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ $service['uri'] }}?{{ isset($service['urlName']) ? $service['urlName'] : 'url' }}={{ urlencode($url) }}<?php if(isset($title) && !empty($title)) { ?><?php echo $sep; ?>{{ isset($service['titleName']) ? $service['titleName'] : 'title' }}={{ urlencode($title) }}<?php } ?><?php if(isset($service['mediaName']) && !empty($media)) { ?><?php echo $sep; ?>{{ $service['mediaName'] }}={{ urlencode($media) }}<?php } ?><?php if (!empty($service['extra'])) { ?><?php echo $sep; ?><?php echo http_build_query($service['extra'], null, $sep) ?><?php } ?>
{{ $service['uri'] }}?{{ isset($service['urlName']) ? $service['urlName'] : 'url' }}={{ rawurlencode($url) }}<?php if(isset($title) && !empty($title)) { ?><?php echo $sep; ?>{{ isset($service['titleName']) ? $service['titleName'] : 'title' }}={{ rawurlencode($title) }}<?php } ?><?php if(isset($service['mediaName']) && !empty($media)) { ?><?php echo $sep; ?>{{ $service['mediaName'] }}={{ rawurlencode($media) }}<?php } ?><?php if (!empty($service['extra'])) { ?><?php echo $sep; ?><?php echo http_build_query($service['extra'], null, $sep, PHP_QUERY_RFC3986) ?><?php } ?>
2 changes: 1 addition & 1 deletion src/views/email.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mailto:?subject={{ urlencode($title) }}&body={{ urlencode($url) }}
mailto:?subject={{ rawurlencode($title) }}&body={{ rawurlencode($url) }}
2 changes: 1 addition & 1 deletion src/views/whatsapp.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
whatsapp://send?text={{ urlencode("$title $url") }}
whatsapp://send?text={{ rawurlencode("$title $url") }}
10 changes: 5 additions & 5 deletions tests/ShareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class ShareTest extends TestCase
"twitter" => "https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Example",
"viadeo" => "http://www.viadeo.com/?url=http%3A%2F%2Fwww.example.com&title=Example",
"vk" => "http://vk.com/share.php?url=http%3A%2F%2Fwww.example.com&title=Example&image=Media&noparse=false",
"whatsapp" => "whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com",
"whatsapp" => "whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com",
"whatsapp" => "whatsapp://send?text=Example%20http%3A%2F%2Fwww.example.com",
"whatsapp" => "whatsapp://send?text=Example%20http%3A%2F%2Fwww.example.com",

"service" => "http://service.example.com?url=http%3A%2F%2Fwww.example.com&title=Example&media=Media",
"service2" => "http://service2.example.com?url=http%3A%2F%2Fwww.example.com&title=Example&extra1=Extra+1&extra2=Extra+2",
"service2" => "http://service2.example.com?url=http%3A%2F%2Fwww.example.com&title=Example&extra1=Extra%201&extra2=Extra%202",
];

protected function getPackageProviders($app)
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testRenderUrlTitleAndMedia()

public function testRenderExtra()
{
$this->assertEquals('http://service2.example.com?url=http%3A%2F%2Fwww.example.com&extra1=Extra+1&extra2=Extra+2',
$this->assertEquals('http://service2.example.com?url=http%3A%2F%2Fwww.example.com&extra1=Extra%201&extra2=Extra%202',
Share::load('http://www.example.com')->service2());
}

Expand Down Expand Up @@ -321,7 +321,7 @@ public function testVk()
*/
public function testWhatsapp()
{
$url = 'whatsapp://send?text=Example+http%3A%2F%2Fwww.example.com';
$url = 'whatsapp://send?text=Example%20http%3A%2F%2Fwww.example.com';
$this->assertEquals($url, Share::load('http://www.example.com', 'Example')->whatsapp());
// $this->assertPageFound($url);
}
Expand Down

0 comments on commit 6fd896e

Please sign in to comment.