/** * @inheritDoc */ public function fetchFolderInfo($folderUrl) { $request = $this->makeRequest($folderUrl); $responseHtml = $this->httpClient->send($request)->getBody()->getContents(); $dom = \pQuery::parseStr($responseHtml); if ($dom->query('#dlnav #path')->count() < 1) { throw new FolderNotFoundException("Folder not found at {$folderUrl}"); } $folderName = ltrim($dom->query('#dlnav #path')->attr('data-path'), '/ '); $files = []; foreach ($dom->query('#filelist .file_name a') as $fileLinkElement) { $fileUrl = $fileLinkElement->attr('href'); $fileName = $fileLinkElement->attr('title'); $files[] = new FshareFile($fileUrl, $fileName); } return new FshareFolder($folderUrl, $folderName, $files); }
/** * Check to see if a string has spoilers and replace them with an innocuous string. * * Good for displaying excerpts from discussions and without showing the spoiler text. * * @param string $html An HTML-formatted string. * @param string $replaceWith The translation code to replace spoilers with. * @return string Returns the html with spoilers removed. */ protected static function replaceSpoilers($html, $replaceWith = '(Spoiler)') { if (preg_match('/class="(User)?Spoiler"/i', $html)) { $htmlDom = pQuery::parseStr($html); foreach ($htmlDom->query('.Spoiler') as $spoilerBlock) { $spoilerBlock->html(t($replaceWith)); } $html = (string) $htmlDom; } return $html; }
/** * Default single discussion display. * * @since 2.0.0 * @access public * * @param int $DiscussionID Unique discussion ID * @param string $DiscussionStub URL-safe title slug * @param int $Page The current page of comments */ public function index($DiscussionID = '', $DiscussionStub = '', $Page = '') { // Setup head $Session = Gdn::session(); $this->addJsFile('jquery.autosize.min.js'); $this->addJsFile('autosave.js'); $this->addJsFile('discussion.js'); Gdn_Theme::section('Discussion'); // Load the discussion record $DiscussionID = is_numeric($DiscussionID) && $DiscussionID > 0 ? $DiscussionID : 0; if (!array_key_exists('Discussion', $this->Data)) { $this->setData('Discussion', $this->DiscussionModel->getID($DiscussionID), true); } if (!is_object($this->Discussion)) { $this->EventArguments['DiscussionID'] = $DiscussionID; $this->fireEvent('DiscussionNotFound'); throw notFoundException('Discussion'); } // Define the query offset & limit. $Limit = c('Vanilla.Comments.PerPage', 30); $OffsetProvided = $Page != ''; list($Offset, $Limit) = offsetLimit($Page, $Limit); // Check permissions. $Category = CategoryModel::categories($this->Discussion->CategoryID); $this->permission('Vanilla.Discussions.View', true, 'Category', val('PermissionCategoryID', $Category, -1)); $this->setData('CategoryID', $this->CategoryID = $this->Discussion->CategoryID, true); if (strcasecmp(val('Type', $this->Discussion), 'redirect') === 0) { $this->redirectDiscussion($this->Discussion); } $this->setData('Category', $Category); if ($CategoryCssClass = val('CssClass', $Category)) { Gdn_Theme::section($CategoryCssClass); } $this->setData('Breadcrumbs', CategoryModel::getAncestors($this->CategoryID)); // Setup $this->title($this->Discussion->Name); // Actual number of comments, excluding the discussion itself. $ActualResponses = $this->Discussion->CountComments; $this->Offset = $Offset; if (c('Vanilla.Comments.AutoOffset')) { // if ($this->Discussion->CountCommentWatch > 1 && $OffsetProvided == '') // $this->addDefinition('ScrollTo', 'a[name=Item_'.$this->Discussion->CountCommentWatch.']'); if (!is_numeric($this->Offset) || $this->Offset < 0 || !$OffsetProvided) { // Round down to the appropriate offset based on the user's read comments & comments per page $CountCommentWatch = $this->Discussion->CountCommentWatch > 0 ? $this->Discussion->CountCommentWatch : 0; if ($CountCommentWatch > $ActualResponses) { $CountCommentWatch = $ActualResponses; } // (((67 comments / 10 perpage) = 6.7) rounded down = 6) * 10 perpage = offset 60; $this->Offset = floor($CountCommentWatch / $Limit) * $Limit; } if ($ActualResponses <= $Limit) { $this->Offset = 0; } if ($this->Offset == $ActualResponses) { $this->Offset -= $Limit; } } else { if ($this->Offset == '') { $this->Offset = 0; } } if ($this->Offset < 0) { $this->Offset = 0; } $LatestItem = $this->Discussion->CountCommentWatch; if ($LatestItem === null) { $LatestItem = 0; } elseif ($LatestItem < $this->Discussion->CountComments) { $LatestItem += 1; } elseif ($LatestItem > $this->Discussion->CountComments) { // If ever the CountCommentWatch is greater than the actual number of comments. $LatestItem = $this->Discussion->CountComments; } $this->setData('_LatestItem', $LatestItem); // Set the canonical url to have the proper page title. $this->canonicalUrl(discussionUrl($this->Discussion, pageNumber($this->Offset, $Limit, 0, false))); // url(ConcatSep('/', 'discussion/'.$this->Discussion->DiscussionID.'/'. Gdn_Format::url($this->Discussion->Name), PageNumber($this->Offset, $Limit, TRUE, Gdn::session()->UserID != 0)), true), Gdn::session()->UserID == 0); // Load the comments $this->setData('Comments', $this->CommentModel->getByDiscussion($DiscussionID, $Limit, $this->Offset)); $PageNumber = PageNumber($this->Offset, $Limit); $this->setData('Page', $PageNumber); $this->_SetOpenGraph(); if ($PageNumber == 1) { $this->description(sliceParagraph(Gdn_Format::plainText($this->Discussion->Body, $this->Discussion->Format), 160)); // Add images to head for open graph $Dom = pQuery::parseStr(Gdn_Format::to($this->Discussion->Body, $this->Discussion->Format)); } else { $this->Data['Title'] .= sprintf(t(' - Page %s'), PageNumber($this->Offset, $Limit)); $FirstComment = $this->data('Comments')->firstRow(); $FirstBody = val('Body', $FirstComment); $FirstFormat = val('Format', $FirstComment); $this->description(sliceParagraph(Gdn_Format::plainText($FirstBody, $FirstFormat), 160)); // Add images to head for open graph $Dom = pQuery::parseStr(Gdn_Format::to($FirstBody, $FirstFormat)); } if ($Dom) { foreach ($Dom->query('img') as $img) { if ($img->attr('src')) { $this->image($img->attr('src')); } } } // Queue notification. if ($this->Request->get('new') && c('Vanilla.QueueNotifications')) { $this->addDefinition('NotifyNewDiscussion', 1); } // Make sure to set the user's discussion watch records if this is not an API request. if ($this->deliveryType() !== DELIVERY_TYPE_DATA) { $this->CommentModel->SetWatch($this->Discussion, $Limit, $this->Offset, $this->Discussion->CountComments); } // Build a pager $PagerFactory = new Gdn_PagerFactory(); $this->EventArguments['PagerType'] = 'Pager'; $this->fireEvent('BeforeBuildPager'); $this->Pager = $PagerFactory->getPager($this->EventArguments['PagerType'], $this); $this->Pager->ClientID = 'Pager'; $this->Pager->configure($this->Offset, $Limit, $ActualResponses, array('DiscussionUrl')); $this->Pager->Record = $this->Discussion; PagerModule::current($this->Pager); $this->fireEvent('AfterBuildPager'); // Define the form for the comment input $this->Form = Gdn::Factory('Form', 'Comment'); $this->Form->Action = url('/post/comment/'); $this->DiscussionID = $this->Discussion->DiscussionID; $this->Form->addHidden('DiscussionID', $this->DiscussionID); $this->Form->addHidden('CommentID', ''); // Look in the session stash for a comment $StashComment = $Session->getPublicStash('CommentForDiscussionID_' . $this->Discussion->DiscussionID); if ($StashComment) { $this->Form->setValue('Body', $StashComment); $this->Form->setFormValue('Body', $StashComment); } // Retrieve & apply the draft if there is one: if (Gdn::session()->UserID) { $DraftModel = new DraftModel(); $Draft = $DraftModel->getByUser($Session->UserID, 0, 1, $this->Discussion->DiscussionID)->firstRow(); $this->Form->addHidden('DraftID', $Draft ? $Draft->DraftID : ''); if ($Draft && !$this->Form->isPostBack()) { $this->Form->setValue('Body', $Draft->Body); $this->Form->setValue('Format', $Draft->Format); } } // Deliver JSON data if necessary if ($this->_DeliveryType != DELIVERY_TYPE_ALL) { $this->setJson('LessRow', $this->Pager->toString('less')); $this->setJson('MoreRow', $this->Pager->toString('more')); $this->View = 'comments'; } // Inform moderator of checked comments in this discussion $CheckedComments = $Session->getAttribute('CheckedComments', array()); if (count($CheckedComments) > 0) { ModerationController::informCheckedComments($this); } // Add modules $this->addModule('DiscussionFilterModule'); $this->addModule('NewDiscussionModule'); $this->addModule('CategoriesModule'); $this->addModule('BookmarkedModule'); $this->CanEditComments = Gdn::session()->checkPermission('Vanilla.Comments.Edit', true, 'Category', 'any') && c('Vanilla.AdminCheckboxes.Use'); // Report the discussion id so js can use it. $this->addDefinition('DiscussionID', $DiscussionID); $this->addDefinition('Category', $this->data('Category.Name')); $this->fireEvent('BeforeDiscussionRender'); $AttachmentModel = AttachmentModel::instance(); if (AttachmentModel::enabled()) { $AttachmentModel->joinAttachments($this->Data['Discussion'], $this->Data['Comments']); $this->fireEvent('FetchAttachmentViews'); if ($this->deliveryMethod() === DELIVERY_METHOD_XHTML) { require_once $this->fetchViewLocation('attachment', 'attachments', 'dashboard'); } } $this->render(); }
/** * * @link http://docs.jquery.com/Utilities/jQuery.map */ public static function map($array, $callback, $param1 = null, $param2 = null, $param3 = null) { $result = array(); $paramStructure = null; if (func_num_args() > 2) { $paramStructure = func_get_args(); $paramStructure = array_slice($paramStructure, 2); } foreach ($array as $v) { $vv = pQuery::callbackRun($callback, array($v), $paramStructure); // $callbackArgs = $args; // foreach($args as $i => $arg) { // $callbackArgs[$i] = $arg instanceof CallbackParam // ? $v // : $arg; // } // $vv = call_user_func_array($callback, $callbackArgs); if (is_array($vv)) { foreach ($vv as $vvv) { $result[] = $vvv; } } else { if ($vv !== null) { $result[] = $vv; } } } return $result; }
/** * Pega o endereço a partir de um determinado CEP. * * @param string $cep Qualquer CEP válido. * @param array $options Possíveis opções que você queira passar ao * adapter que estiver sendo utilizado para realizar as requisições ao * webservice. * * @throws \InvalidArgumentException Se o CEP tiver valor inválido. * @throws \RuntimeException Se nenhum endereço for encontrado. * * @return array */ public function endereco($cep, array $options = []) { $cep = str_replace('-', '', $cep); if (strlen($cep) !== 8 || !is_numeric($cep)) { throw new \InvalidArgumentException('Use um valor de CEP válido!'); } $body = ['cepEntrada' => $cep, 'tipoCep' => '', 'cepTemp' => '', 'metodo' => 'buscarCep']; $html = $this->httpAdapter->post($this->endpoints['cep'], $body, $options); $html = \pQuery::parseStr($html); $parts = $html->query('span.respostadestaque'); $addressParts = []; $address = []; foreach ($parts as $p) { $addressParts[] = $p->getPlainText(); } if (empty($addressParts)) { throw new \RuntimeException('Nenhum endereço encontrado com este CEP!'); } $partsCount = count($addressParts); switch ($partsCount) { case 2: list($cidade, $estado) = explode('/', $addressParts[0]); $address = ['logradouro' => null, 'logradouro_extra' => null, 'bairro' => null, 'cidade' => utf8_encode(trim($cidade)), 'uf' => utf8_encode(trim($estado)), 'cep' => utf8_encode(trim($addressParts[1]))]; break; default: list($cidade, $estado) = explode('/', $addressParts[2]); $address = ['logradouro' => utf8_encode(trim($addressParts[0])), 'logradouro_extra' => null, 'bairro' => utf8_encode(trim($addressParts[1])), 'cidade' => utf8_encode(trim($cidade)), 'uf' => utf8_encode(trim($estado)), 'cep' => utf8_encode(trim($addressParts[3]))]; if (strpos($address['logradouro'], ' - ') !== false) { $extra = explode(' - ', $address['logradouro']); $street = array_shift($extra); $extra = implode(' - ', $extra); $address['logradouro'] = trim($street); $address['logradouro_extra'] = trim($extra); } break; } return $address; }
/** * Get the images from a DOM. * * @param pQuery $dom The DOM to search. * @param string $url The URL of the document to add to relative URLs. * @param int $maxImages The maximum number of images to return. * @return array Returns an array in the form: `[['Src' => '', 'Width' => '', 'Height' => ''], ...]`. */ function domGetImages($dom, $url, $maxImages = 4) { $Images = array(); foreach ($dom->query('img') as $element) { $Images[] = array('Src' => absoluteSource($element->attr('src'), $url), 'Width' => $element->attr('width'), 'Height' => $element->attr('height')); } // Gdn::Controller()->Data['AllImages'] = $Images; // Sort by size, biggest one first $ImageSort = array(); // Only look at first 4 images (speed!) $i = 0; foreach ($Images as $ImageInfo) { $Image = $ImageInfo['Src']; if (strpos($Image, 'doubleclick.') != false) { continue; } try { if ($ImageInfo['Height'] && $ImageInfo['Width']) { $Height = $ImageInfo['Height']; $Width = $ImageInfo['Width']; } else { list($Width, $Height) = getimagesize($Image); } $Diag = (int) floor(sqrt($Width * $Width + $Height * $Height)); // Gdn::Controller()->Data['Foo'][] = array($Image, $Width, $Height, $Diag); if (!$Width || !$Height) { continue; } // Require min 100x100 dimension image. if ($Width < 100 && $Height < 100) { continue; } // Don't take a banner-shaped image. if ($Height * 4 < $Width) { continue; } // Prefer images that are less than 800px wide (banners?) // if ($Diag > 141 && $Width < 800) { } if (!array_key_exists($Diag, $ImageSort)) { $ImageSort[$Diag] = array($Image); } else { $ImageSort[$Diag][] = $Image; } $i++; if ($i > $maxImages) { break; } } catch (Exception $ex) { // do nothing } } krsort($ImageSort); $GoodImages = array(); foreach ($ImageSort as $Diag => $Arr) { $GoodImages = array_merge($GoodImages, $Arr); } return $GoodImages; }