$index = 0;
 $ObjUid = str_replace('"', '', $aux[$index++]);
 if (isset($_GET['v'])) {
     $versionReq = $_GET['v'];
 }
 //downloading the file
 $localPath = PATH_DOCUMENT . 'input' . PATH_SEP;
 G::mk_dir($localPath);
 $newfilename = G::GenerateUniqueId() . '.pm';
 $downloadUrl = PML_DOWNLOAD_URL . '?id=' . $ObjUid . (isset($_GET['s']) ? '&s=' . $_GET['s'] : '');
 //print "<hr>$downloadUrl<hr>";die;
 G::LoadClass('processes');
 $oProcess = new Processes();
 $oProcess->downloadFile($downloadUrl, $localPath, $newfilename);
 //getting the ProUid from the file recently downloaded
 $oData = $oProcess->getProcessData($localPath . $newfilename);
 if (is_null($oData)) {
     throw new Exception('Error');
 }
 $Fields['IMPORT_OPTION'] = 2;
 $Fields['PRO_FILENAME'] = $newfilename;
 $Fields['OBJ_UID'] = $ObjUid;
 $sProUid = $oData->process['PRO_UID'];
 $oData->process['PRO_UID_OLD'] = $sProUid;
 //print $sProUid;die;
 //if the process exists, we need to ask what kind of re-import the user wants,
 if ($oProcess->processExists($sProUid)) {
     $G_MAIN_MENU = 'processmaker';
     $G_ID_MENU_SELECTED = 'PROCESSES';
     $G_PUBLISH = new Publisher();
     $G_PUBLISH->AddContent('xmlform', 'xmlform', 'processes/processes_ImportExisting', '', $Fields, 'downloadPML_ImportExisting');
 /**
  * import process fromLibrary: downloads and imports a process from the ProcessMaker library
  *
  * @param string sessionId : The session ID (which was obtained at login).
  * @param string processId :
  * @param string version :
  * @param string importOption :
  * @param string usernameLibrary : The username to obtain access to the ProcessMaker library.
  * @param string passwordLibrary : The password to obtain access to the ProcessMaker library.
  * @return $eturns will return an object
  */
 public function importProcessFromLibrary($processId, $version = '', $importOption = '', $usernameLibrary = '', $passwordLibrary = '')
 {
     try {
         G::LoadClass('processes');
         //$versionReq = $_GET['v'];
         //. (isset($_GET['s']) ? '&s=' . $_GET['s'] : '')
         $ipaddress = $_SERVER['REMOTE_ADDR'];
         $oProcesses = new Processes();
         $oProcesses->ws_open_public();
         $oProcess = $oProcesses->ws_processGetData($processId);
         if ($oProcess->status_code != 0) {
             throw new Exception($oProcess->message);
         }
         $privacy = $oProcess->privacy;
         $strSession = '';
         if ($privacy != 'FREE') {
             global $sessionId;
             $antSession = $sessionId;
             $oProcesses->ws_open($usernameLibrary, $passwordLibrary);
             $strSession = "&s=" . $sessionId;
             $sessionId = $antSession;
         }
         //downloading the file
         $localPath = PATH_DOCUMENT . 'input' . PATH_SEP;
         G::mk_dir($localPath);
         $newfilename = G::GenerateUniqueId() . '.pm';
         $downloadUrl = PML_DOWNLOAD_URL . '?id=' . $processId . $strSession;
         $oProcess = new Processes();
         $oProcess->downloadFile($downloadUrl, $localPath, $newfilename);
         //getting the ProUid from the file recently downloaded
         $oData = $oProcess->getProcessData($localPath . $newfilename);
         if (is_null($oData)) {
             $data['DOWNLOAD_URL'] = $downloadUrl;
             $data['LOCAL_PATH'] = $localPath;
             $data['NEW_FILENAME'] = $newfilename;
             throw new Exception(G::loadTranslation('ID_ERROR_URL_PROCESS_INVALID', SYS_LANG, $data));
         }
         $sProUid = $oData->process['PRO_UID'];
         $oData->process['PRO_UID_OLD'] = $sProUid;
         //if the process exists, we need to check the $importOption to and re-import if the user wants,
         if ($oProcess->processExists($sProUid)) {
             //Update the current Process, overwriting all tasks and steps
             if ($importOption == 1) {
                 $oProcess->updateProcessFromData($oData, $localPath . $newfilename);
                 //delete the xmlform cache
                 if (file_exists(PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP . $sProUid)) {
                     $oDirectory = dir(PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP . $sProUid);
                     while ($sObjectName = $oDirectory->read()) {
                         if ($sObjectName != '.' && $sObjectName != '..') {
                             $strAux = PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP;
                             $strAux = $strAux . $sProUid . PATH_SEP . $sObjectName;
                             unlink($strAux);
                         }
                     }
                     $oDirectory->close();
                 }
                 $sNewProUid = $sProUid;
             }
             //Disable current Process and create a new version of the Process
             if ($importOption == 2) {
                 $oProcess->disablePreviousProcesses($sProUid);
                 $sNewProUid = $oProcess->getUnusedProcessGUID();
                 $oProcess->setProcessGuid($oData, $sNewProUid);
                 $oProcess->setProcessParent($oData, $sProUid);
                 $oData->process['PRO_TITLE'] = "New - " . $oData->process['PRO_TITLE'] . ' - ' . date('M d, H:i');
                 $oProcess->renewAll($oData);
                 $oProcess->createProcessFromData($oData, $localPath . $newfilename);
             }
             //Create a completely new Process without change the current Process
             if ($importOption == 3) {
                 //krumo ($oData); die;
                 $sNewProUid = $oProcess->getUnusedProcessGUID();
                 $oProcess->setProcessGuid($oData, $sNewProUid);
                 $strAux = "Copy of  - " . $oData->process['PRO_TITLE'] . ' - ' . date('M d, H:i');
                 $oData->process['PRO_TITLE'] = $strAux;
                 $oProcess->renewAll($oData);
                 $oProcess->createProcessFromData($oData, $localPath . $newfilename);
             }
             if ($importOption != 1 && $importOption != 2 && $importOption != 3) {
                 throw new Exception(G::loadTranslation('ID_PROCESS_ALREADY_IN_SYSTEM'));
             }
         }
         //finally, creating the process if the process doesn't exist
         if (!$oProcess->processExists($processId)) {
             $oProcess->createProcessFromData($oData, $localPath . $newfilename);
         }
         //show the info after the imported process
         $oProcess = new Processes();
         $oProcess->ws_open_public();
         $processData = $oProcess->ws_processGetData($processId);
         $result->status_code = 0;
         $result->message = G::loadTranslation('ID_COMMAND_EXECUTED_SUCCESSFULLY');
         $result->timestamp = date('Y-m-d H:i:s');
         $result->processId = $processId;
         $result->processTitle = $processData->title;
         $result->category = isset($processData->category) ? $processData->category : '';
         $result->version = $processData->version;
         return $result;
     } catch (Exception $e) {
         $result = new wsResponse(100, $e->getMessage());
         return $result;
     }
 }
 //load the variables
 G::LoadClass('processes');
 $oProcess = new Processes();
 if (!isset($_POST['form']['IMPORT_OPTION'])) {
     throw new Exception('Please select an option before to continue');
 }
 if (!isset($_POST['form']['GROUP_IMPORT_OPTION'])) {
     $action = "none";
 } else {
     $action = $_POST['form']['GROUP_IMPORT_OPTION'];
 }
 $option = $_POST['form']['IMPORT_OPTION'];
 $filename = $_POST['form']['PRO_FILENAME'];
 $ObjUid = $_POST['form']['OBJ_UID'];
 $path = PATH_DOCUMENT . 'input' . PATH_SEP;
 $oData = $oProcess->getProcessData($path . $filename);
 $Fields['PRO_FILENAME'] = $filename;
 $sProUid = $oData->process['PRO_UID'];
 $oData->process['PRO_UID_OLD'] = $sProUid;
 // code added by gustavo cruz gustavo-at-colosa-dot-com
 // evaluate actions or import options
 switch ($action) {
     case "none":
         $groupsDuplicated = $oProcess->checkExistingGroups($oData->groupwfs);
         break;
     case "rename":
         $oData->groupwfs = $oProcess->renameExistingGroups($oData->groupwfs);
         $groupsDuplicated = $oProcess->checkExistingGroups($oData->groupwfs);
         break;
     case "merge":
         $oBaseGroup = $oData->groupwfs;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
 * Coral Gables, FL, 33134, USA, or email info@colosa.com.
 */
G::LoadClass('Installer');
$inst = new Installer();
G::LoadClass('processes');
$oProcess = new Processes();
//Get Available autoinstall process
$availableProcess = $inst->getDirectoryFiles(PATH_OUTTRUNK . "autoinstall", "pm");
$path = PATH_OUTTRUNK . "autoinstall" . PATH_SEP;
$message = "";
foreach ($availableProcess as $processfile) {
    $oData = $oProcess->getProcessData($path . $processfile);
    $Fields['PRO_FILENAME'] = $processfile;
    $Fields['IMPORT_OPTION'] = 2;
    $sProUid = $oData->process['PRO_UID'];
    if ($oProcess->processExists($sProUid)) {
        $message .= "{$processfile} - Not imported (process exist)<br>";
    } else {
        $oProcess->createProcessFromData($oData, $path . $processfile);
        $message .= "{$processfile} - OK<br>";
    }
}
echo $message;