private function removeGallery(Gallery $gallery) { $webPath = $this->get('kernel')->getRootDir() . '/../web/'; $folderPrefixes = array('data/', 'media/cache/cache/data/', 'media/cache/preview_thumb/data/', 'media/cache/slider_thumb/data/'); foreach ($folderPrefixes as $folderPrefix) { $targetDirectory = realpath($webPath . $folderPrefix . $gallery->getUploader()->getId() . '/' . $gallery->getId()); if ($targetDirectory && is_dir($targetDirectory) && strlen($targetDirectory) > 20) { // just in case shell_exec('rm -Rf ' . $targetDirectory); } } $em = $this->getDoctrine()->getManager(); foreach ($gallery->getImages() as $galleryImage) { $em->remove($galleryImage); } $em->flush(); return true; }
private function downloadGallery($gid) { try { $em = $this->doctrine->getManager(); # Fetch all images $buf = $this->getFromUrl(_URL . 'pictures/' . $gid . '/?view=2'); preg_match_all('#border=0 src="(.+?)">#', $buf, $m); if (count($m[1]) < 1) { throw new \Exception('no images found'); } $galleryImages = $m[1]; $this->writeSessionData('INFO: Found ' . count($galleryImages) . ' images'); # Fetch gallery title preg_match('#<title>P**n pics of (.+?) \\(Page (?:[0-9]+)\\)</title>#', $buf, $m); if (!$m[1]) { throw new \Exception('no gallery title'); } $galleryTitle = $m[1]; $this->writeSessionData('INFO: Found Title "' . $galleryTitle . '"'); # Fetch uploader's name preg_match('#<b><font size="3" color="\\#CC0000">Uploaded by (.+?)</font></b>#', $buf, $m); /*if(!$m[1]) throw new \Exception('no uploader found'); $galleryUploader = $m[1];*/ if ($m[1]) { $galleryUploader = $m[1]; } else { $galleryUploader = 'Anonymous'; $this->writeSessionData('WARN: No Uploader found, using Anonymous'); } $this->writeSessionData('INFO: Found Uploader "' . $galleryUploader . '"'); $this->writeSessionData('INFO: Started Download'); # Checking for existing uploader /** * @var \Ifportal\DatabaseBundle\Entity\Uploader $uploader */ $uploader = $this->doctrine->getRepository('DatabaseBundle:Uploader')->findOneBy(array('name' => $galleryUploader)); # If not, create uploader if (!$uploader) { $uploader = new Uploader(); $uploader->setName($galleryUploader); $em->persist($uploader); $em->flush(); $this->writeSessionData('INFO: Uploader added'); } # Checking for existing gallery /** * @var \Ifportal\DatabaseBundle\Entity\Gallery $gallery */ $gallery = $this->doctrine->getRepository('DatabaseBundle:Gallery')->findOneBy(array('uploader' => $uploader->getId(), 'title' => $galleryTitle)); if ($gallery && $gallery->getBlocked()) { $this->writeSessionData('INFO: Gallery blocked'); return true; } # If not, create gallery if (!$gallery) { $gallery = new Gallery(); $gallery->setTitle($galleryTitle); $gallery->setUploader($uploader); $em->persist($gallery); $em->flush(); $this->writeSessionData('INFO: Gallery added'); } else { $this->writeSessionData('INFO: Gallery already exists'); } $targetPath = $this->kerneldir . '/../web/data/' . $uploader->getId() . '/' . $gallery->getId() . '/'; @mkdir($targetPath, 0777, true); $galleryImagesDatabase = array(); foreach ($gallery->getImages() as $galleryImageDB) { $galleryImagesDatabase[] = $galleryImageDB->getName(); } foreach ($galleryImages as $galleryImage) { $imageFilename = pathinfo($galleryImage, PATHINFO_BASENAME); $galleryImageHQ = str_replace('thumb', 'full', $galleryImage); if (in_array($imageFilename, $galleryImagesDatabase)) { $this->writeSessionData('NOTICE: File already exists ("' . $imageFilename . '")'); continue; } $tempfile = $this->downloadTemporary($galleryImageHQ); $fetchStatus = false; if (!$this->isImage($tempfile)) { $tempfile = $this->downloadTemporary($galleryImageHQ); if ($this->isImage($tempfile)) { $fetchStatus = true; } elseif (strpos(file_get_contents($tempfile), 'html') > 0) { preg_match("#img src='(.+?)'>#", file_get_contents($tempfile), $m); if ($m[1]) { $tempfile = $this->downloadTemporary($galleryImageHQ); if ($this->isImage($tempfile)) { $fetchStatus = true; } else { $this->writeSessionData('ERROR: Cannot fetch "' . $imageFilename . '" (' . $galleryImageHQ . ') #1'); } } else { $this->writeSessionData('ERROR: Cannot fetch "' . $imageFilename . '" (' . $galleryImageHQ . ') #2'); } } else { $this->writeSessionData('ERROR: Cannot fetch "' . $imageFilename . '" (' . $galleryImageHQ . ') #3'); } } else { $fetchStatus = true; } if ($fetchStatus) { $copyStatus = rename($tempfile, $targetPath . $imageFilename); if ($copyStatus) { $this->writeSessionData('SUCCESS: Saved "' . $imageFilename . '"'); @chmod($targetPath . $imageFilename, 0775); $image = new Image(); $image->setName($imageFilename); $image->setGallery($gallery); $em->persist($image); $em->flush(); } else { $this->writeSessionData('ERROR: Cannot save "' . $imageFilename . '" (' . $targetPath . $imageFilename . ')'); } } else { $this->writeSessionData('ERROR: Cannot fetch "' . $imageFilename . '" (' . $galleryImageHQ . ') #4'); } } return true; } catch (\Exception $e) { $this->writeSessionData('ERROR: ' . $e->getMessage()); } }