2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * Class to create a zipped patch package
16 * zipped patch package contains patch.xml, the files to be added, overwritten
19 * @package PatchCreator
22 define('TR_INCLUDE_PATH', '../../include/');
23 require_once (TR_INCLUDE_PATH.'vitals.inc.php');
25 require_once(TR_INCLUDE_PATH. "../updater/include/patch_xml_template.inc.php");
26 require_once(TR_INCLUDE_PATH."classes/DAO/MyownPatchesDAO.class.php");
27 require_once(TR_INCLUDE_PATH."classes/DAO/MyownPatchesDependentDAO.class.php");
28 require_once(TR_INCLUDE_PATH."classes/DAO/MyownPatchesFilesDAO.class.php");
33 var $patch_info_array = array(); // the patch info data
34 var $patch_xml_file; // location of patch.xml
35 var $current_patch_id; // current myown_patches.myown_patch_id
36 var $version_folder; // version folder. underneath updater content folder, to hold all patch folders and corresponding upload files
37 var $patch_folder; // patch folder. underneath version folder, to hold all upload files
39 var $myownPatchesDAO; // DAO object for table "myown_patches"
40 var $myownPatchesDependentDAO; // DAO object for table "myown_patches_dependent"
41 var $myownPatchesFilesDAO; // DAO object for table "myown_patches_files"
44 * Constructor: Initialize object members
47 * @param $patch_info_array All information to create a patch. Example:
50 * [system_patch_id] => Patch001
51 * [transformable_version_to_apply] => 1.6
52 * [description] => this is a sample patch info array
54 * [dependent_patches] => Array
63 * [file_name] => script1.php
65 * [directory] => admin/
66 * [upload_tmp_name] => C:\xampp\tmp\php252.tmp
71 * [file_name] => script2.php
73 * [directory] => tools/
80 function PatchCreator($patch_info_array, $patch_id)
82 // add slashes if magic_quotes_gpc is off
83 if (!get_magic_quotes_gpc())
85 $patch_info_array["description"] = addslashes($patch_info_array["description"]);
86 $patch_info_array["sql_statement"] = addslashes($patch_info_array["sql_statement"]);
88 for ($i = 0; $i < count($patch_info_array["files"]); $i++)
90 $patch_info_array["files"][$i]["directory"] = addslashes($patch_info_array["files"][$i]["directory"]);
91 $patch_info_array["files"][$i]["upload_tmp_name"] = addslashes($patch_info_array["files"][$i]["upload_tmp_name"]);
92 $patch_info_array["files"][$i]["code_from"] = addslashes($patch_info_array["files"][$i]["code_from"]);
93 $patch_info_array["files"][$i]["code_to"] = addslashes($patch_info_array["files"][$i]["code_to"]);
97 $this->patch_info_array = $patch_info_array;
98 $this->current_patch_id = $patch_id;
100 $this->patch_xml_file = TR_CONTENT_DIR . "updater/patch.xml";
102 $this->version_folder = TR_CONTENT_DIR . "updater/" . str_replace('.', '_', $this->patch_info_array["transformable_version_to_apply"]) . "/";
103 $this->patch_folder = $this->version_folder . $this->patch_info_array["system_patch_id"] . "/";
105 $this->myownPatchesDAO = new MyownPatchesDAO();
106 $this->myownPatchesDependentDAO = new MyownPatchesDependentDAO();
107 $this->myownPatchesFilesDAO = new MyownPatchesFilesDAO();
113 * @return true if created successfully
114 * false if error happens
115 * @author Cindy Qi Li
117 function create_patch()
119 // save patch info into database & save uploaded files into content folder
122 // create patch.xml into $this->patch_xml_file
123 $fp = fopen($this->patch_xml_file,'w');
124 fwrite($fp,$this->createXML());
127 // create zip package and force download
131 unlink($this->patch_xml_file);
137 * Save patch info into database & save uploaded files into content folder
140 * @author Cindy Qi Li
146 if ($this->current_patch_id == 0)
148 $this->current_patch_id = $this->myownPatchesDAO->Create(
149 $this->patch_info_array["system_patch_id"],
150 $this->patch_info_array["transformable_version_to_apply"],
151 $this->patch_info_array["description"],
152 $this->patch_info_array["sql_statement"]);
156 $this->myownPatchesDAO->Update($this->current_patch_id,
157 $this->patch_info_array["system_patch_id"],
158 $this->patch_info_array["transformable_version_to_apply"],
159 $this->patch_info_array["description"],
160 $this->patch_info_array["sql_statement"]);
163 if ($this->current_patch_id == 0)
165 $this->current_patch_id = mysql_insert_id();
167 else // delete records for current_patch_id in tables myown_patches_dependent & myown_patches_files
169 $this->myownPatchesDependentDAO->DeleteByPatchID($this->current_patch_id);
170 $this->myownPatchesFilesDAO->DeleteByPatchID($this->current_patch_id);
173 // insert records into table myown_patches_dependent
174 if (is_array($this->patch_info_array["dependent_patches"]))
176 foreach ($this->patch_info_array["dependent_patches"] as $dependent_patch)
178 $this->myownPatchesDependentDAO->Create($this->current_patch_id, $dependent_patch);
182 // insert records into table myown_patches_files
183 if (is_array($this->patch_info_array["files"]))
185 foreach ($this->patch_info_array["files"] as $file_info)
187 if ($file_info["upload_tmp_name"] <> "")
188 $upload_to = $this->saveFile($file_info);
192 $this->myownPatchesFilesDAO->Create($this->current_patch_id,
193 $file_info["action"],
194 $file_info["file_name"],
195 $file_info["directory"],
196 $file_info["code_from"],
197 $file_info["code_to"],
204 * Save upload file into content folder
207 * @author Cindy Qi Li
209 function saveFile($file_info)
211 // mkdir() function cannot create folder recursively, so have to acomplish the creation of patch folder by 2 steps.
212 if (!file_exists($this->version_folder)) mkdir($this->version_folder);
213 if (!file_exists($this->patch_folder)) mkdir($this->patch_folder);
215 $upload_to = $this->patch_folder . $file_info['file_name'];
217 // copy uploaded file into content folder
218 copy($file_info["upload_tmp_name"], $upload_to);
220 return realpath($upload_to);
227 * @author Cindy Qi Li
231 global $patch_xml, $dependent_patch_xml;
232 global $patch_action_detail_xml, $patch_file_xml;
234 // generate content of <dependent_patches> section
235 if (is_array($this->patch_info_array["dependent_patches"]))
237 foreach ($this->patch_info_array["dependent_patches"] as $dependent_patch)
238 $dependent_patches .= str_replace('{DEPENDENT_PATCH}', $dependent_patch, $dependent_patch_xml);
241 // generate content of <files> section
242 if (is_array($this->patch_info_array["files"]))
244 foreach ($this->patch_info_array["files"] as $file_info)
246 $action_details = "";
248 if ($file_info["action"] == "alter")
250 $action_details .= str_replace(array('{TYPE}', '{CODE_FROM}', '{CODE_TO}'),
252 htmlspecialchars(stripslashes($file_info["code_from"]), ENT_QUOTES),
253 htmlspecialchars(stripslashes($file_info["code_to"]), ENT_QUOTES)),
254 $patch_action_detail_xml);
257 $xml_files .= str_replace(array('{ACTION}', '{NAME}', '{LOCATION}', '{ACTION_DETAILS}'),
258 array($file_info["action"], $file_info["file_name"], $file_info["directory"], $action_details),
263 // generate patch.xml
264 return str_replace(array('{system_patch_id}',
268 '{DEPENDENT_PATCHES}',
270 array($this->patch_info_array["system_patch_id"],
271 $this->patch_info_array["transformable_version_to_apply"],
272 htmlspecialchars(stripslashes($this->htmlNewLine($this->patch_info_array["description"])), ENT_QUOTES),
273 htmlspecialchars(stripslashes($this->patch_info_array["sql_statement"]), ENT_QUOTES),
280 * Create xml for section <files> in patch.xml.
283 * @author Cindy Qi Li
285 function createXMLFiles($file_info)
288 $action_details = "";
290 if ($file_info["action"] == "alter")
292 $action_details .= str_replace(array('{TYPE}', '{CODE_FROM}', '{CODE_TO}'),
294 htmlspecialchars(stripslashes($file_info["code_from"]), ENT_QUOTES),
295 htmlspecialchars(stripslashes($file_info["code_to"]), ENT_QUOTES)),
296 $patch_action_detail_xml);
299 return str_replace(array('{ACTION}', '{NAME}', '{LOCATION}', '{ACTION_DETAILS}'),
300 array($file_info["action"], $file_info["file_name"], $file_info["directory"], $action_details),
305 * Create zip file which contains patch.xml and the files to be added, overwritten, altered; and force to download
307 * @return true if successful
309 * @author Cindy Qi Li
313 require_once(TR_INCLUDE_PATH . '/classes/zipfile.class.php');
315 $zipfile = new zipfile();
317 $zipfile->add_file(file_get_contents($this->patch_xml_file), 'patch.xml');
319 if (is_array($this->patch_info_array["files"]))
321 foreach ($this->patch_info_array["files"] as $file_info)
323 if ($file_info["upload_tmp_name"] <> '')
325 $file_name = preg_replace('/.php$/', '.new', $file_info['file_name']);
326 $zipfile->add_file(file_get_contents($file_info['upload_tmp_name']), $file_name);
331 $zipfile->send_file($this->patch_info_array["system_patch_id"]);
335 * replace new line string to html tag <br />
337 * @return converted string
338 * @author Cindy Qi Li
340 function htmlNewLine($str)
342 $new_line_array = array("\n", "\r", "\n\r", "\r\n");
344 $found_match = false;
346 if (strlen(trim($str))==0) return "";
348 foreach ($new_line_array as $new_line)
349 if (preg_match('/'.preg_quote($new_line).'/', $str) > 0)
351 $search_new_line = $new_line;
356 return preg_replace('/'. preg_quote($search_new_line) .'/', "<br />", $str);