silverstripe /
silverstripe-sharedraftcontent
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace SilverStripe\ShareDraftContent\Extensions; |
||||||
| 4 | |||||||
| 5 | use SilverStripe\CMS\Model\SiteTree; |
||||||
| 6 | use SilverStripe\Control\Controller; |
||||||
| 7 | use SilverStripe\Control\Director; |
||||||
| 8 | use SilverStripe\Core\Config\Config; |
||||||
| 9 | use SilverStripe\ORM\DataExtension; |
||||||
| 10 | use SilverStripe\ORM\HasManyList; |
||||||
| 11 | use SilverStripe\Security\RandomGenerator; |
||||||
| 12 | use SilverStripe\ShareDraftContent\Models\ShareToken; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * @property SiteTree|ShareDraftContentSiteTreeExtension $owner |
||||||
| 16 | * @property string $ShareTokenSalt |
||||||
| 17 | * @method HasManyList|ShareToken[] ShareTokens() |
||||||
| 18 | */ |
||||||
| 19 | class ShareDraftContentSiteTreeExtension extends DataExtension |
||||||
| 20 | { |
||||||
| 21 | /** |
||||||
| 22 | * The number of days a shared link should be valid for, before expiring. |
||||||
| 23 | * |
||||||
| 24 | * @config |
||||||
| 25 | * |
||||||
| 26 | * @var int |
||||||
| 27 | */ |
||||||
| 28 | private static $valid_for_days = 30; |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * @var array |
||||||
| 32 | */ |
||||||
| 33 | private static $db = array( |
||||||
|
0 ignored issues
–
show
|
|||||||
| 34 | 'ShareTokenSalt' => 'Varchar(16)', |
||||||
| 35 | ); |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * @var array |
||||||
| 39 | */ |
||||||
| 40 | private static $has_many = array( |
||||||
|
0 ignored issues
–
show
|
|||||||
| 41 | 'ShareTokens' => ShareToken::class, |
||||||
| 42 | ); |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * @var array |
||||||
| 46 | */ |
||||||
| 47 | private static $allowed_actions = array( |
||||||
|
0 ignored issues
–
show
|
|||||||
| 48 | 'MakeShareDraftLink' |
||||||
| 49 | ); |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * @return string |
||||||
| 53 | */ |
||||||
| 54 | public function ShareTokenLink() |
||||||
| 55 | { |
||||||
| 56 | $shareToken = $this->getNewShareToken(); |
||||||
| 57 | |||||||
| 58 | return Controller::join_links( |
||||||
| 59 | Director::absoluteBaseURL(), |
||||||
| 60 | 'preview', |
||||||
| 61 | $this->generateKey($shareToken->Token), |
||||||
| 62 | $shareToken->Token |
||||||
| 63 | ); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * @return ShareToken |
||||||
| 68 | */ |
||||||
| 69 | protected function getNewShareToken() |
||||||
| 70 | { |
||||||
| 71 | if (!$this->owner->ShareTokenSalt) { |
||||||
| 72 | $this->owner->ShareTokenSalt = $this->getNewToken(); |
||||||
| 73 | $this->owner->writeWithoutVersion(); |
||||||
|
0 ignored issues
–
show
The method
writeWithoutVersion() does not exist on SilverStripe\ShareDraftC...ontentSiteTreeExtension.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | $found = null; |
||||||
| 77 | $token = null; |
||||||
| 78 | $tries = 1; |
||||||
| 79 | $limit = 5; |
||||||
| 80 | |||||||
| 81 | while (!$found && ($tries++ < $limit)) { |
||||||
| 82 | $token = $this->getNewToken(); |
||||||
| 83 | |||||||
| 84 | $found = ShareToken::get()->filter(array( |
||||||
| 85 | "Token" => $token, |
||||||
| 86 | "PageID" => $this->owner->ID, |
||||||
|
0 ignored issues
–
show
|
|||||||
| 87 | ))->first(); |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | $config = Config::forClass(__CLASS__); |
||||||
| 91 | |||||||
| 92 | $validForDays = $config->get('valid_for_days'); |
||||||
| 93 | |||||||
| 94 | $token = ShareToken::create(array( |
||||||
| 95 | "Token" => $token, |
||||||
| 96 | "ValidForDays" => $validForDays, |
||||||
| 97 | "PageID" => $this->owner->ID, |
||||||
| 98 | )); |
||||||
| 99 | |||||||
| 100 | $token->write(); |
||||||
| 101 | |||||||
| 102 | return $token; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 103 | } |
||||||
| 104 | |||||||
| 105 | /** |
||||||
| 106 | * @return string |
||||||
| 107 | */ |
||||||
| 108 | protected function getNewToken() |
||||||
| 109 | { |
||||||
| 110 | $generator = new RandomGenerator(); |
||||||
| 111 | |||||||
| 112 | return substr($generator->randomToken('sha256'), 0, 16); |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | /** |
||||||
| 116 | * @param string $salt |
||||||
| 117 | * |
||||||
| 118 | * @return string |
||||||
| 119 | */ |
||||||
| 120 | public function generateKey($salt) |
||||||
| 121 | { |
||||||
| 122 | return hash_pbkdf2('sha256', $salt, $this->owner->ShareTokenSalt, 1000, 16); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * @return string |
||||||
| 127 | */ |
||||||
| 128 | public function getShareDraftLinkAction() |
||||||
| 129 | { |
||||||
| 130 | return $this->owner->Link('MakeShareDraftLink'); |
||||||
|
0 ignored issues
–
show
The method
Link() does not exist on SilverStripe\ShareDraftC...ontentSiteTreeExtension.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 |