Source of file DownloadLink.php
Size: 2,439 Bytes - Last Modified: 2021-12-23T10:03:22+00:00
/var/www/docs.ssmods.com/process/src/code/DownloadLink.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | <?php /** * Associates a link hash to a given file and order. This could be * overkill, but it does keep the hash from being guessable and/or * taking a long time to look up. * * @author Mark Guinn <mark@adaircreative.com> * @date 10.28.2013 * @package shop_downloadable */ class DownloadLink extends DataObject { private static $db = array( 'Hash' => 'Varchar(255)', ); private static $has_one = array( 'File' => 'File', 'Order' => 'Order', ); private static $indexes = array( 'Hash' => true, ); /** * @param int|File $file * @param int|Order $order * @return DownloadLink */ public static function generate($file, $order) { $file = is_object($file) ? $file->ID : $file; $order = is_object($order) ? $order->ID : $order; $rec = new DownloadLink; $rec->Hash = sha1(uniqid() . $file . $order); $rec->FileID = $file; $rec->OrderID = $order; $rec->write(); return $rec; } /** * @param int|File $file * @param int|Order $order * @return DownloadLink */ public static function find_or_make($file, $order) { $rec = DownloadLink::get()->filter(array( 'FileID' => is_object($file) ? $file->ID : $file, 'OrderID' => is_object($order) ? $order->ID : $order, ))->first(); return ($rec && $rec->exists()) ? $rec : self::generate($file, $order); } /** * @param string $hash * @return DownloadLink */ public static function get_by_hash($hash) { return self::get()->filter(array('Hash' => $hash))->first(); } /** * @return string */ public function getURL() { return Config::inst()->get('Downloadable', 'download_link_base') . '/' . $this->Hash; } /** * @return string */ public function getAbsoluteURL() { return Director::absoluteURL($this->getURL()); } /** * This is just here for guessability for template authors * @return string */ public function Link() { return $this->getURL(); } /** * This is just here for guessability for template authors * @return string */ public function AbsoluteLink() { return $this->getAbsoluteURL(); } } |