Created
February 4, 2013 21:56
-
-
Save branquito/4710069 to your computer and use it in GitHub Desktop.
JOOMLA1.5--ScriptMoveImages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @version $Id: view.html.php 11393 2009-01-05 02:11:06Z ian $ | |
* @package Joomla | |
* @subpackage Contact | |
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved. | |
* @license GNU/GPL, see LICENSE.php | |
* Joomla! is free software. This version may have been modified pursuant to the | |
* GNU General Public License, and as distributed it includes or is derivative | |
* of works licensed under the GNU General Public License or other free or open | |
* source software licenses. See COPYRIGHT.php for copyright notices and | |
* details. | |
*/ | |
// Check to ensure this file is included in Joomla! | |
defined('_JEXEC') or die( 'Restricted access' ); | |
jimport('joomla.application.component.view'); | |
//Import filesystem libraries. Perhaps not necessary, but does not hurt | |
jimport('joomla.filesystem.folder'); | |
jimport('joomla.filesystem.file'); | |
/** | |
* @package Joomla | |
* @subpackage Contacts | |
*/ | |
class ContactViewSlike extends JView | |
{ | |
function display($tpl = null) | |
{ | |
global $mainframe; | |
// IMAGE-MOVE SCRIPT | |
// | |
// | |
// Get a db connection. | |
$passes = Array( | |
'introtext', 'fulltext' | |
); | |
$db = JFactory::getDbo(); | |
// Create a new query object. | |
// $query = $db->getQuery(true); | |
$delete_paths = Array(); | |
foreach ($passes as $a_pass) { | |
# code... | |
// ..doing query here | |
$query = "select a.id, a.created, `a`.`" . $a_pass . "`, b.title from #__content AS a INNER JOIN #__categories AS b ON a.catid = b.id ORDER BY a.created ASC"; | |
// Reset the query using our newly populated query object. | |
$db->setQuery($query); | |
// Load the results as a list of stdClass objects. | |
$results = $db->loadObjectList(); | |
foreach ($results as $result) { | |
// parse article as HTML into DOMDocument object | |
$doc = new DOMDocument(); | |
$doc->loadHTML($result->$a_pass); | |
$img_found = false; | |
// get IMG tags only | |
$image_tags = $doc->getElementsByTagName('img'); | |
foreach ($image_tags as $one_tag) { | |
// echo "<b> $result->id </b>" . " " . "<b> $result->title </b>" . "<br>"; | |
// echo "Kreirano: " . $result->created . "<br>"; | |
// echo "OLD PATH : "; | |
// extract IMG src attr.. | |
$img_path = $one_tag->getAttribute('src'); | |
$filename = basename($img_path); | |
// echo $img_path . "<br>"; | |
// extract FILENAME only, so we now have a file to be copied.. | |
// echo $filename . "<br>"; | |
// try regex.. | |
// | |
// format is 2013-02-01.. more general yyyy-mm-dd.. | |
// we will extract year and month | |
// add this prefix to all paths where you want to store your images.. | |
$prefix_path = 'images/stories/'; | |
// regex which will remember first four digits [year], when the article was created.. | |
$pattern = "/(\d{4})-\d{2}/"; | |
$subject = $result->created; | |
// var_dump($subject); | |
// if image exists do copy and change src attr.. | |
if (file_exists($img_path)) { | |
// var_dump($result); | |
echo "<span style=\"color: green;\">image exists<br></span>"; | |
// .. change <img src=''> attrib | |
// then copy image before going to the next one .. | |
preg_match($pattern, $subject, $matches); | |
$my_path = $prefix_path . $matches[1] . "/" . JFilterOutput::stringURLSafe($result->title) . "/"; | |
$new_intro = str_replace($img_path, $my_path . $filename, $result->$a_pass); | |
$result->$a_pass = $new_intro; | |
echo var_dump($new_intro); | |
// change OS specific delimiter so the COPY command will work as should | |
$img_path = str_replace("/", DS, $img_path); | |
$my_path = str_replace("/", DS, $my_path); | |
echo "Image copied from : " . $img_path . " to " . $my_path; | |
echo "<br />"; | |
echo "<br />"; | |
// Create Folder if Does Not Exist | |
if (!is_dir(JPATH_BASE . DS . $my_path)){ | |
$created = JFolder::create(JPATH_BASE.DS.$my_path); | |
} | |
// COPY IMAGE | |
if (copy(JPATH_BASE.DS.$img_path, JPATH_BASE.DS.$my_path.$filename)){ | |
$delete_paths[]=JPATH_BASE.DS.$img_path; | |
// unlink(JPATH_BASE.DS.$img_path); | |
} | |
// copy(JPATH_BASE.DS.$img_path, JPATH_BASE.DS.$my_path.$filename); | |
// TEST NEW PATH | |
// echo "<span style=\"font-size: 23px\">$my_path</span>"; | |
// echo var_dump($matches); | |
// echo "<br>"; | |
// set flag for update query to know when to update an article | |
$img_found = true; | |
} | |
// IMAGE DOES NOT EXIST, TELL USER! | |
else { | |
echo "<span style=\"color: red;\">image does not exist<br></span>"; | |
} | |
} | |
// if article contains image/images do update content | |
if ($img_found){ | |
// echo(JPATH_BASE . DS . $img_path . "," . JPATH_BASE . DS . $my_path.$filename); | |
// echo "<BR>"; | |
// echo "<br>"; | |
// Create a new update query object. | |
$query_update = $db->getQuery(true); | |
// ..doing query here | |
$query_update = 'update #__content SET `' . $a_pass . '` = "' . mysql_real_escape_string($new_intro) . '" WHERE id = "' . $result->id . '"'; | |
$db->setQuery($query_update); | |
$db->query(); | |
} | |
} | |
} | |
foreach ($delete_paths as $path_to_delete) { | |
// var_dump($path_to_delete); | |
unlink($path_to_delete); | |
} | |
echo "Deleted " . count($delete_paths) . " images"; | |
$this->assignRef('contact', $contact); | |
$this->assignRef('contacts', $contacts); | |
$this->assignRef('params', $pparams); | |
parent::display($tpl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment