public function create($quantity, $uid) { $numbers = new Ml_Model_Numbers(); $tokens = array(); for ($counter = 0; $counter < $quantity; $counter++) { //not beautiful $partialFirst = $numbers->baseEncode(mt_rand(36 * 36 + 1, 36 * 36 * 36), "qwertyuiopasdfghjklzxcvbnm0123456789"); $partialSecond = $numbers->baseEncode(mt_rand(31 * 31 + 1, 31 * 31 * 31), "qwrtyuopasdghjklzcvnm123456789"); $tokens[] = $partialFirst . '-' . $partialSecond; } $escapeUid = $this->_dbAdapter->quoteInto("?", $uid); if (!empty($tokens)) { $querystring = "INSERT IGNORE INTO `" . $this->_dbTable->getTableName() . "` (`uid`, `hash`) VALUES "; do { $line = '(' . $escapeUid . ', ' . $this->_dbAdapter->quoteInto("?", current($tokens)) . ')'; $querystring .= $line; next($tokens); if (current($tokens)) { $querystring .= ", "; } } while (current($tokens)); $this->_dbAdapter->query($querystring); } return $tokens; }
/** * * Creates the short link * @param big int $shareId * @param bool $escape */ public function shortLink($shareId) { $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); $numbers = new Ml_Model_Numbers(); $base58Id = $numbers->base58Encode($shareId); $link = $config['URLshortening']['addr'] . $base58Id; return $link; }
public function testDecodeBase58() { $numbers = new Ml_Model_Numbers(); $results = array(); foreach ($this->_translations10to58base as $base10 => $base58) { $testNum = $numbers->base58Decode($base58); $results[$testNum] = $base58; } $this->assertEquals($this->_translations10to58base, $results, "Failure in decoding from base58"); }
public static function form() { static $form = ''; $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); $numbers = new Ml_Model_Numbers(); if (!is_object($form)) { $router = Zend_Controller_Front::getInstance()->getRouter(); $shareInfo = $registry->get('shareInfo'); $userInfo = $registry->get('userInfo'); $form = new Ml_Form_Tweet(array('action' => $router->assemble(array("username" => $userInfo['alias'], "share_id" => $shareInfo['id']), "sharepage_1stpage") . '?tweet', 'method' => 'post')); } $form->setDefault("hash", $registry->get('globalHash')); $form->setDefault("tweet", $shareInfo['title'] . ' ' . $config['URLshortening']['twitterlink'] . $numbers->base58Encode($shareInfo['id'])); return $form; }
/** * Makes a pseudo-uniquely ID * * Format: * x-y-z * * whereas: * x - time portion * y - random number * z - check digit * * There's a collision chance that must be avoided * with integrity check where necessary. * */ public static function makeUUId() { $numbers = new Ml_Model_Numbers(); $lowerLimit = pow(29, 3); //==2111 $upperLimit = pow(29, 4) - 1; //==ZZZZ $timeDivisor = 2.5; $ptime = (int) ((int) $_SERVER['REQUEST_TIME'] / $timeDivisor); $rand1 = mt_rand($lowerLimit, $upperLimit); $rand2 = mt_rand($lowerLimit, $upperLimit); $num1 = $numbers->baseEncode($ptime, self::base); $num2 = $numbers->baseEncode($rand1, self::base); $num3 = $numbers->baseEncode($rand2, self::base); $uuid = $num1 . $num2 . $num3 . Ml_Model_Verhoeff::calcsum($ptime . $rand1 . $rand2); return $uuid; }
public function getUploadStatus($uid) { $numbers = new Ml_Model_Numbers(); $config = self::$_registry->get("config"); if (!$numbers->isNaturalDbId($uid)) { return false; } $this->_dbAdapter->query("set @aaaab:=0"); $since = date("Y-m-00 00:00:00"); $call = "select sum from (select * from(select @aaaab:=@aaaab+filesize as sum, id from upload_history where byUid = ? and timestamp >= ? order by timestamp ASC) as sizesum) as sum order by sum DESC LIMIT 1"; $query = $this->_dbAdapter->fetchOne($call, array($uid, $since)); if (!$query) { $query = 0; } $this->_dbAdapter->query("set @aaaab:=0"); $uploadStatus = array("bandwidth" => array("maxbytes" => floor($config['share']['monthlyLimit'] * 1024 * 1024), "usedbytes" => ceil($query), "remainingbytes" => floor($config['share']['monthlyLimit'] * 1024 * 1024 - $query)), "filesize" => array("maxbytes" => floor($config['share']['maxFileSize'] * 1024 * 1024))); return $uploadStatus; }
public function shortLink() { $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); $uri = $_SERVER['REQUEST_URI']; if ($uri == '/') { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://" . $config['webhost'] . "/"); exit; } //clear the first and the last '/' if (mb_substr($uri, -1) == '/') { $uri = mb_substr($uri, 1, -1); } else { $uri = mb_substr($uri, 1); } $numbers = new Ml_Model_Numbers(); $id = $numbers->base58Decode($uri); if ($id) { //Is it a valid share ID? $share = Ml_Model_Share::getInstance(); $people = Ml_Model_People::getInstance(); $shareInfo = $share->getById($id); if ($shareInfo) { $userInfo = $people->getById($shareInfo['byUid']); $link = "http://" . $config['webhost'] . "/" . urlencode($userInfo['alias']) . "/" . $shareInfo['id']; header("HTTP/1.1 301 Moved Permanently"); header("Location: " . $link); exit; //nothing more to do } } //If nothing matches $link = "http://" . $config['webhost'] . "/not-found/" . urlencode(utf8_encode($uri)); header("Location: " . $link); //the redirector stops the default bootstrap, always exit; }
public function infoAction() { //@todo route: do it the right way! $router = new Zend_Controller_Router_Rewrite(); $routeConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/defaultRoutes.ini'); $router->addConfig($routeConfig, 'routes'); $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); $request = $this->getRequest(); $params = $request->getParams(); $people = Ml_Model_People::getInstance(); $favorites = Ml_Model_Favorites::getInstance(); $comments = Ml_Model_Comments::getInstance(); $tags = Ml_Model_Tags::getInstance(); $numbers = new Ml_Model_Numbers(); $this->_helper->loadApiresource->share(); $shareInfo = $registry->get("shareInfo"); $userInfo = $people->getById($shareInfo['byUid']); $tagsList = $tags->getShareTags($shareInfo['id']); $countFavs = $favorites->count($shareInfo['id']); $countComments = $comments->count($shareInfo['id']); //begin of response $doc = new Ml_Model_Dom(); $doc->formatOutput = true; $rootElement = $doc->createElement("file"); $doc->appendChild($rootElement); $rootElement->appendChild($doc->newTextAttribute('id', $shareInfo['id'])); $rootElement->appendChild($doc->newTextAttribute('secret', $shareInfo['secret'])); $rootElement->appendChild($doc->newTextAttribute('download_secret', $shareInfo['download_secret'])); $ownerElement = $doc->createElement("owner"); $ownerData = array("id" => $userInfo['id'], "username" => $userInfo['alias'], "realname" => $userInfo['name']); foreach ($ownerData as $field => $data) { $ownerElement->appendChild($doc->newTextAttribute($field, $data)); } $rootElement->appendChild($ownerElement); $shareData = array("title" => $shareInfo['title'], "filename" => $shareInfo['filename'], "filetype" => $shareInfo['type'], "short" => $shareInfo['short'], "description" => $shareInfo['description_filtered'], "url" => "http://" . $config['webhost'] . $router->assemble(array("username" => $userInfo['alias'], "share_id" => $shareInfo['id']), "sharepage_1stpage"), "dataurl" => $config['services']['S3']['sharesBucketAddress'] . $userInfo['alias'] . "/" . $shareInfo['id'] . "-" . $shareInfo['download_secret'] . "/" . $shareInfo['filename'], "shorturl" => $config['URLshortening']['addr'] . $numbers->base58Encode($shareInfo['id']), "comments" => $countComments, "favorites" => $countFavs); foreach ($shareData as $field => $data) { $rootElement->appendChild($doc->newTextElement($field, $data)); } $filesizeElement = $doc->createElement("filesize"); $filesizeElement->appendChild($doc->newTextAttribute("bits", $shareInfo['fileSize'])); $filesizeElement->appendChild($doc->newTextAttribute("kbytes", ceil($shareInfo['fileSize'] / (1024 * 8)))); $rootElement->appendChild($filesizeElement); $checksumElement = $doc->createElement("checksum"); $checksumElement->appendChild($doc->newTextAttribute("hash", "md5")); $checksumElement->appendChild($doc->newTextAttribute("value", $shareInfo['md5'])); $rootElement->appendChild($checksumElement); $visibilityElement = $doc->createElement("visibility"); $visibilityElement->appendChild($doc->newTextAttribute("ispublic", "1")); $rootElement->appendChild($visibilityElement); $datesData = array("posted" => $shareInfo['uploadedTime'], "lastupdate" => $shareInfo['lastChange']); $datesElement = $doc->createElement("dates"); foreach ($datesData as $field => $data) { $datesElement->appendChild($doc->newTextAttribute($field, $data)); } $rootElement->appendChild($datesElement); $tagsElement = $doc->createElement("tags"); foreach ($tagsList as $tag) { $tagElement = $doc->createElement("tag"); $tagElement->appendChild($doc->newTextAttribute("id", $tag['id'])); $tagElement->appendChild($doc->newTextAttribute("raw", $tag['raw'])); $tagElement->appendChild($doc->createTextNode($tag['clean'])); $tagsElement->appendChild($tagElement); } $rootElement->appendChild($tagsElement); $this->_helper->printResponse($doc); }
/** * Matches a user submitted path. Assigns and returns an array of variables * on a successful match. * * If a request object is registered, it uses its setModuleName(), * setControllerName(), and setActionName() accessors to set those values. * Always returns the values as an array. * * @param string $path Path used to match against this routing map * @return array An array of assigned values or a false on a mismatch */ public function match($path, $partial = false) { $numbers = new Ml_Model_Numbers(); if (HOST_MODULE == "api") { return array("controller" => "notstatic", "action" => "error", "module" => "api"); } else { if (HOST_MODULE == "default") { //@todo this is a workaround for another resource: the tag system //the Zend_Controller_Router_Route_Regex don't work with utf-8 //despite trying the hack http://framework.zend.com/issues/browse/ZF-6661 //it didn't work //clear this part of the code when everything is ok //and only let the static=>docs $path = explode("/", $path, 5); if (isset($path[2]) && $path[2] == "tags" && isset($path[3])) { //could be using regex... $username = $path[1]; if ($username) { $tag = urldecode($path[3]); if (!isset($path[4])) { $page = "1"; } else { if (mb_substr($path[4], 0, 4) == "page") { $tryPage = mb_substr($path[4], 4); if ($numbers->isNaturalDbId($tryPage)) { $page = $tryPage; } } } if (isset($page)) { return array("username" => $username, "tag" => $tag, "page" => $page, "controller" => "tagspages", "action" => "tagpage", "module" => "default"); } } } //end of workaround return array("controller" => "static", "action" => "docs", "module" => "default"); } } $this->_setRequestKeys(); $values = array(); $params = array(); if (!$partial) { $path = trim($path, self::URI_DELIMITER); } else { $matchedPath = $path; } if ($path != '') { $path = explode(self::URI_DELIMITER, $path); if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) { $values[$this->_moduleKey] = array_shift($path); $this->_moduleValid = true; } if (count($path) && !empty($path[0])) { $values[$this->_controllerKey] = array_shift($path); } if (count($path) && !empty($path[0])) { $values[$this->_actionKey] = array_shift($path); } if ($numSegs = count($path)) { for ($i = 0; $i < $numSegs; $i = $i + 2) { $key = urldecode($path[$i]); $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val; } } } if ($partial) { $this->setMatchedPath($matchedPath); } $this->_values = $values + $params; return $this->_values + $this->_defaults; }