Add option to grab images into same dir or subdir

This commit is contained in:
Laurent Destailleur 2018-02-19 14:56:37 +01:00
parent 60fe1fa503
commit 60a77a6d85
4 changed files with 315 additions and 276 deletions

View File

@ -222,9 +222,11 @@ function includeContainer($containeralias)
* @param string $tmp Content to parse * @param string $tmp Content to parse
* @param string $action Var $action * @param string $action Var $action
* @param string $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage * @param string $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage
* @param int $grabimages 0=Do not grab images, 1=Grab images
* @param string $grabimagesinto 'root' or 'subpage'
* @return void * @return void
*/ */
function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks=0) function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks=0, $grabimages=1, $grabimagesinto='subpage')
{ {
global $conf; global $conf;
@ -252,15 +254,18 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
} }
$linkwithoutdomain = $regs[2][$key]; $linkwithoutdomain = $regs[2][$key];
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.'/'.$objectpage->pageurl.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key]; $dirforimages = '/'.$objectpage->pageurl;
if ($grabimagesinto == 'root') $dirforimages='';
// Define $filetosave and $filename
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key];
if (preg_match('/^http/', $regs[2][$key])) if (preg_match('/^http/', $regs[2][$key]))
{ {
$urltograbbis = $regs[2][$key]; $urltograbbis = $regs[2][$key];
$linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]); $linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]);
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.'/'.$objectpage->pageurl.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain; $filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
} }
$filename = 'image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
$filename = 'image/'.$object->ref.'/'.$objectpage->pageurl.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
// Clean the aa/bb/../cc into aa/cc // Clean the aa/bb/../cc into aa/cc
$filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave); $filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave);
@ -271,6 +276,8 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
//exit; //exit;
if (empty($alreadygrabbed[$urltograbbis])) if (empty($alreadygrabbed[$urltograbbis]))
{
if ($grabimages)
{ {
$tmpgeturl = getURLContent($urltograbbis); $tmpgeturl = getURLContent($urltograbbis);
if ($tmpgeturl['curl_error_no']) if ($tmpgeturl['curl_error_no'])
@ -298,6 +305,7 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
@chmod($filetosave, octdec($conf->global->MAIN_UMASK)); @chmod($filetosave, octdec($conf->global->MAIN_UMASK));
} }
} }
}
if ($modifylinks) if ($modifylinks)
{ {
@ -343,6 +351,8 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
//exit; //exit;
if (empty($alreadygrabbed[$urltograbbis])) if (empty($alreadygrabbed[$urltograbbis]))
{
if ($grabimages)
{ {
$tmpgeturl = getURLContent($urltograbbis); $tmpgeturl = getURLContent($urltograbbis);
if ($tmpgeturl['curl_error_no']) if ($tmpgeturl['curl_error_no'])
@ -370,6 +380,7 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
@chmod($filetosave, octdec($conf->global->MAIN_UMASK)); @chmod($filetosave, octdec($conf->global->MAIN_UMASK));
} }
} }
}
if ($modifylinks) if ($modifylinks)
{ {

View File

@ -69,3 +69,8 @@ WEBSITE_USE_WEBSITE_ACCOUNTSTooltip=Enable the table to store web site accounts
YouMustDefineTheHomePage=You must first define the default Home page YouMustDefineTheHomePage=You must first define the default Home page
OnlyEditionOfSourceForGrabbedContentFuture=Note: only edition of HTML source will be possible when a page content is intiliazed by grabbing it from an external page (WYSIWYG editor will not be available) OnlyEditionOfSourceForGrabbedContentFuture=Note: only edition of HTML source will be possible when a page content is intiliazed by grabbing it from an external page (WYSIWYG editor will not be available)
OnlyEditionOfSourceForGrabbedContent=Only edition of HTML source is possible when content was grabber from an external site OnlyEditionOfSourceForGrabbedContent=Only edition of HTML source is possible when content was grabber from an external site
GrabImagesInto=Grab also images found into css and page.
ImagesShouldBeSavedInto=Images should be saved into directory
WebsiteRootOfImages=Root directory for website images
SubdirOfPage=Sub-directory dedicated to page
AliasPageAlreadyExists=Alias page <strong>%s</strong> already exists

View File

@ -113,13 +113,16 @@ class WebsitePage extends CommonObject
*/ */
public function create(User $user, $notrigger = false) public function create(User $user, $notrigger = false)
{ {
$this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
$this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
return $this->createCommon($user, $notrigger); return $this->createCommon($user, $notrigger);
} }
/** /**
* Load object in memory from the database * Load object in memory from the database
* *
* @param int $id Id object. If this is 0, the default page of website_id will be used, if not defined, the first one found. * @param int $id Id object. If this is 0, the value into $page will be used. If not found of $page not defined, the default page of website_id will be used or the first page found if not set.
* @param string $website_id Web site id (page name must also be filled if this parameter is used) * @param string $website_id Web site id (page name must also be filled if this parameter is used)
* @param string $page Page name (website id must also be filled if this parameter is used) * @param string $page Page name (website id must also be filled if this parameter is used)
* *

View File

@ -261,6 +261,9 @@ if ($action == 'addcontainer')
if (GETPOST('fetchexternalurl','alpha')) if (GETPOST('fetchexternalurl','alpha'))
{ {
$urltograb=GETPOST('externalurl','alpha'); $urltograb=GETPOST('externalurl','alpha');
$grabimages=GETPOST('grabimages','alpha');
$grabimagesinto=GETPOST('grabimagesinto','alpha');
//var_dump($grabimages);exit;
} }
if ($urltograb) if ($urltograb)
@ -275,12 +278,25 @@ if ($action == 'addcontainer')
{ {
$urltograb.='/'; $urltograb.='/';
} }
$pageurl = dol_sanitizeFileName(preg_replace('/[\/\.]/','-', preg_replace('/\/+$/', '', $urltograbwithoutdomainandparam)));
$urltograbdirwithoutslash = dirname($urltograb.'.'); $urltograbdirwithoutslash = dirname($urltograb.'.');
$urltograbdirrootwithoutslash = getRootURLFromURL($urltograbdirwithoutslash); $urltograbdirrootwithoutslash = getRootURLFromURL($urltograbdirwithoutslash);
// Exemple, now $urltograbdirwithoutslash is https://www.dolimed.com/screenshots // Exemple, now $urltograbdirwithoutslash is https://www.dolimed.com/screenshots
// and $urltograbdirrootwithoutslash is https://www.dolimed.com // and $urltograbdirrootwithoutslash is https://www.dolimed.com
// Check pageurl is not already used
$tmpwebsitepage = new WebsitePage($db);
$result = $tmpwebsitepage->fetch(0, $object->id, $pageurl);
if ($result > 0)
{
setEventMessages($langs->trans("AliasPageAlreadyExists", $pageurl), null, 'errors');
$error++;
$action='createcontainer';
}
if (! $error)
{
$tmp = getURLContent($urltograb); $tmp = getURLContent($urltograb);
if ($tmp['curl_error_no']) if ($tmp['curl_error_no'])
{ {
@ -303,7 +319,7 @@ if ($action == 'addcontainer')
$head = $reg[1]; $head = $reg[1];
$objectpage->type_container = 'page'; $objectpage->type_container = 'page';
$objectpage->pageurl = dol_sanitizeFileName(preg_replace('/[\/\.]/','-', preg_replace('/\/+$/', '', $urltograbwithoutdomainandparam))); $objectpage->pageurl = $pageurl;
if (empty($objectpage->pageurl)) if (empty($objectpage->pageurl))
{ {
$tmpdomain = getDomainFromURL($urltograb); $tmpdomain = getDomainFromURL($urltograb);
@ -479,7 +495,7 @@ if ($action == 'addcontainer')
// $filename = 'image/'.$object->ref.'/'.$objectpage->pageurl.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain; // $filename = 'image/'.$object->ref.'/'.$objectpage->pageurl.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
$pagecsscontent.='/* Content of file '.$urltograbbis.' */'."\n"; $pagecsscontent.='/* Content of file '.$urltograbbis.' */'."\n";
getAllImages($object, $objectpage, $urltograbbis, $tmpgeturl['content'], $action, 1); getAllImages($object, $objectpage, $urltograbbis, $tmpgeturl['content'], $action, 1, $grabimages, $grabimagesinto);
$pagecsscontent.=$tmpgeturl['content']."\n"; $pagecsscontent.=$tmpgeturl['content']."\n";
@ -494,10 +510,10 @@ if ($action == 'addcontainer')
$objectpage->htmlheader .= $pagecsscontent; $objectpage->htmlheader .= $pagecsscontent;
// Now loop to fetch all images // Now loop to fetch all images into page
$tmp = $objectpage->content; $tmp = $objectpage->content;
getAllImages($object, $objectpage, $urltograb, $tmp, $action, 1); getAllImages($object, $objectpage, $urltograb, $tmp, $action, 1, $grabimages, $grabimagesinto);
//print dol_escape_htmltag($tmp);exit; //print dol_escape_htmltag($tmp);exit;
$objectpage->content = $tmp; $objectpage->content = $tmp;
@ -505,6 +521,7 @@ if ($action == 'addcontainer')
$objectpage->grabbed_from = $urltograb; $objectpage->grabbed_from = $urltograb;
} }
} }
}
else else
{ {
$objectpage->type_container = GETPOST('WEBSITE_TYPE_CONTAINER','alpha'); $objectpage->type_container = GETPOST('WEBSITE_TYPE_CONTAINER','alpha');
@ -1895,8 +1912,6 @@ if ($action == 'editmeta' || $action == 'createcontainer')
print '<!-- Edit or create page/container -->'."\n"; print '<!-- Edit or create page/container -->'."\n";
//print '<div class="fichecenter">'; //print '<div class="fichecenter">';
if ($conf->global->MAIN_FEATURES_LEVEL >= 1)
{
if ($action == 'createcontainer') if ($action == 'createcontainer')
{ {
print '<br>'; print '<br>';
@ -1907,8 +1922,14 @@ if ($action == 'editmeta' || $action == 'createcontainer')
print $langs->trans("URL"); print $langs->trans("URL");
print '</td><td>'; print '</td><td>';
print '<input class="flat minwidth300" type="text" name="externalurl" value="'.dol_escape_htmltag(GETPOST('externalurl','alpha')).'" placeholder="https://externalsite/pagetofetch"> '; print '<input class="flat minwidth300" type="text" name="externalurl" value="'.dol_escape_htmltag(GETPOST('externalurl','alpha')).'" placeholder="https://externalsite/pagetofetch"> ';
print '<input class="button" type="submit" name="fetchexternalurl" value="'.dol_escape_htmltag($langs->trans("FetchAndCreate")).'">'; print '<input class="flat paddingtop" type="checkbox" name="grabimages" value="1" checked="checked"> '.$langs->trans("GrabImagesInto");
print '<br><br>'.info_admin($langs->trans("OnlyEditionOfSourceForGrabbedContentFuture"), 0, 0, '1'); print ' ';
print $langs->trans("ImagesShouldBeSavedInto").' ';
$arraygrabimagesinto=array('root'=>$langs->trans("WebsiteRootOfImages"), 'subpage'=>$langs->trans("SubdirOfPage"));
print $form->selectarray('grabimagesinto', $arraygrabimagesinto, GETPOSTISSET('grabimagesinto')?GETPOST('grabimagesinto'):'root');
print '<br>';
print '<input class="button" style="margin-top: 5px" type="submit" name="fetchexternalurl" value="'.dol_escape_htmltag($langs->trans("FetchAndCreate")).'">';
print '<br>'.info_admin($langs->trans("OnlyEditionOfSourceForGrabbedContentFuture"), 0, 0, '1');
print '</td></tr>'; print '</td></tr>';
print '</table>'; print '</table>';
@ -1916,7 +1937,6 @@ if ($action == 'editmeta' || $action == 'createcontainer')
print ' * '.$langs->trans("OrEnterPageInfoManually").'<br><hr>'; print ' * '.$langs->trans("OrEnterPageInfoManually").'<br><hr>';
} }
}
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';