Alfresco “admin” password keeps getting deleted when i call the same webscript multiple times

StackOverflow https://stackoverflow.com/questions/5324813

  •  25-10-2019
  •  | 
  •  

Question

I've got a weird problem. I've created a procedure in PHP that does these things:

  1. Read data (9.000 rows) from a MySQL table
  2. Writes a row to another table
  3. Create a folder in Alfresco based on a field in the row

I've got this strange problem (only on one Alfresco installation, on others I don't have the problem), after the first 2-3 rows the password for the admin user gets deleted from Alfresco (if you look in the db, you can't find it) and so the procedure stops. I'm using Alfresco Community 3.2.

Here is the PHP code of the procedure:

   require_once("../components/com_astra/libs/AlfrescoConnect.php");
require_once("../components/com_astra/libs/FirePHPCore/fb.php");
$username = "***";
$password = "****;";
$hostname = "localhost"; 
ob_start();
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("lbs_8",$dbhandle)
  or die("Could not select examples");
$result = mysql_query("SELECT count(*) as quanti FROM ast_tabellaTemporaneaNdg");
if (!$result) {
    die('ERRORE: ' . mysql_error());
}
$totaleTabella = mysql_result($result, 0);
echo("La tabella ast_tabellaTemporaneaNdg contiene [$totaleTabella] records<br>");
echo("Conto il numero di record della vista di transcodifica<br>");
$result = mysql_query("SELECT count(*) as quanti FROM vista_ndg_normalizzati");
if (!$result) {
    die('ERRORE: ' . mysql_error());
}
$totaleVista = mysql_result($result, 0);
echo("La vista vista_ndg_normalizzati contiene [$totaleVista] records<br>");
if ($totaleTabella == $totaleVista){
    echo("Tutte le righe della tabella ast_tabellaTemporaneaNdg sono transcodificate correttamente<br>");
    echo("Saranno quindi importati [$totaleTabella] records<br>");
}else{
    echo("Non tutte le righe della tabella ast_tabellaTemporaneaNdg sono transcodificate correttamente<br>");
    echo("nella tabella ast_tabellaTemporaneaNdg sono presenti [$totaleTabella] records, di questi solo [$totaleVista] transcodificano correttamente<br>");
    $handle = fopen("ndgNonImportabili.txt", "w");
    $result = mysql_query("SELECT ndg FROM ast_tabellaTemporaneaNdg WHERE ndg NOT IN (SELECT ndg from vista_ndg_normalizzati)");
    if (!$result) {
        die('ERRORE: ' . mysql_error());
    }
    while ($row = mysql_fetch_assoc($result)){
        $ndg = "NDG: ".$row["ndg"]."\n";
        fwrite ($handle, $ndg);

    }
    fclose($handle);
    echo("Gli NDG non importabili sono stati loggati nel file ndgNonImportabili.txt<br>");
    //die("L'importazione avra' inizio solo quando tutti gli NDG saranno importabili. I motivi per cui un ndg non è importabile possono essere: incorenza fra Natura Giuridica 1 e natura giuridica 2, Incoerenza fra comune, Provincia e Regione<br>");
}
$result = mysql_query("SELECT * FROM vista_ndg_normalizzati");
//fb($result, 'risultati');
$handle = fopen("logImportazione.txt", "w");
while ($row = mysql_fetch_assoc($result)) {
    $user = "admin";
    $ndg = $row['ndg'];
    $response = AlfrescoConnect::createNDG($ndg, $user);
    $status = $response['esito'];
    if($status == "OK"){
        $ragioneSociale = mysql_real_escape_string($row['ragioneSociale']);
        $query = "SET FOREIGN_KEY_CHECKS=0";
        $resultSetKeys = mysql_query($query);
        $query = "INSERT INTO ast_Cliente (idUtente, idNaturaGiuridica,  idTipoSegmento, idRegione, idProvincia, idComune,  ragioneSociale, ndg, idNaturaGiuridica2) VALUES ";
        $query .= " (1, ".$row['idNaturaGiuridica'].", 2 , ".$row['idRegione'].", ".$row['idProvincia'].", ".$row['idComune'].", '".$ragioneSociale."', '".$ndg."', ".$row['idNaturaGiuridica2'].")";
        //fb($query);
        $resultInsert = mysql_query($query);
        if (!$resultInsert) {
            $messaggio = "Si è verificato un errore nell'importazione dell'ndg [$ndg]: [".mysql_error()."]\n";
        }else{
            $messaggio = "[$ndg] importato con successo\n";
        }
        fwrite ($handle, $messaggio);
    }else{
        $messaggio = "Si è verificato un errore nella creazione della cartella per l'ndg [$ndg] su alfresco. ERRORE: [".$response['motivazione']."]\n";
        fwrite ($handle, $messaggio);

    }
   }
fclose($handle);
echo("importazione completata: i risultati dell'importazione sono stati salvati nel file logImportazione.txt");
?> 

The folder is created by calling $response = AlfrescoConnect::createNDG($ndg, $user); where $ndg is the name of the folder and $user is the user who created it

Here is the code of AlfrescoConnect.php:

class AlfrescoConnect
{

    static public $webscriptURL = "http://localhost:8080/alfresco/service";
    static public $login = "/api/login";
    static public $login_ticket = "/api/login/ticket";
    static public $alfrescoPath = "http://localhost:8080";

    static public $alfrescoContext = "/alfresco";

    static public $upload_file_script = "/astra/upload";
    static public $uploadFile = "/astra/uploaddoc";
    static public $search_document = "/astra/cercadocumenti";
    static public $createNDG = "/astra/creandg";
    static public $createPEFPerNDG = "/astra/creapef";
    static public $searchDocumentPerConfirm = "/astra/cercadocumentoperconferma";
    static public $confirmDocument = "/astra/confermadocumenti";
    static public $getPEF = "/astra/estraidatipef";
    static public $updatePEF = "/astra/aggiornapef";
    static public $documentCounter = "/astra/contadocumenti";
    static public $userLogin = "/astra/creautente";
    static public $cancella = "/astra/managedocumentversions";
    static public $alfUsername = "admin";
    static public $alfPassword = "admin";

    static public $ticket = "";
    static public $userTicket = ""; 

    static public function Authenticate($username='') {
        //fb($username);
        if(strcmp($username,'')==0){
            if (AlfrescoConnect::isAuthenticated()) return true;
            $link = AlfrescoConnect::$webscriptURL.AlfrescoConnect::$login."?u=".AlfrescoConnect::$alfUsername."&pw=".AlfrescoConnect::$alfPassword . "&noCache=" .time();
            fb($link, "LINK");
        }else{
            AlfrescoConnect::$userTicket =  AlfrescoConnect::CustomAuthentication($username);
            return true;
        }
        try {

            //perform a http/get request to get tiket
            // Try to create a ticket
            // If the ticket fails, it means that the username and/or password are wrong
            $r= new HttpRequest($link, HttpRequest::METH_GET);
            $r->send() ;
            $http_response = $r->getResponseBody();fb($http_response, "RISPOSTA");
            $pos1 = stripos($http_response, "<ticket>");
            $pos2 = stripos($http_response, "</ticket>");
            //ceck if token is returned 
            if ($pos1<0 | !is_int($pos1) || $pos2<0 | !is_int($pos2)) {
                AlfrescoConnect::$ticket = "";
                $returnValue = false;
            } else {

                $pos1 += 8;
                $pos2 -= $pos1;
                // clean the xml info
                AlfrescoConnect::$ticket = substr($http_response, $pos1, $pos2);
                $returnValue = true;
            }
            //fb(AlfrescoConnect::$ticket);
            $_SESSION["ticket"] = AlfrescoConnect::$ticket;
            return true;

        }
        catch (Exception $e) {
            fb($e, "Eccezione");
            AlfrescoConnect::$ticket = "";
            $_SESSION["ticket"] = AlfrescoConnect::$ticket;
            return $returnValue;
        }

    }
    static public function isAuthenticated() {
        if (!isset($_SESSION["ticket"])) return false;
        $link = AlfrescoConnect::$webscriptURL.AlfrescoConnect::$login_ticket."/".$_SESSION["ticket"]."?alf_ticket=".$_SESSION["ticket"]."&noCache=" .time();
        fb($link, 'link in isauth');
        try {

            //perform a http/get request to get tiket
            // Try to create a ticket
            // If the ticket fails, it means that the username and/or password are wrong
            $r= new HttpRequest($link, HttpRequest::METH_GET);
            $r->send() ;
            $http_response = $r->getResponseBody();

            $pos1 = stripos($http_response, "<ticket>");
            $pos2 = stripos($http_response, "</ticket>");
            //ceck if token is returned 
            if ($pos1<0 | !is_int($pos1) || $pos2<0 | !is_int($pos2)) {
                return false;
            } else {

                $pos1 += 8;
                $pos2 -= $pos1;
                // clean the xml info
                if ($_SESSION["ticket"] == substr($http_response, $pos1, $pos2)) {
                    AlfrescoConnect::$ticket = $_SESSION["ticket"];
                    return true;
                }
            }
        }
        catch (Exception $e) {
            return false;
        }
    }

    static public function CustomAuthentication($username=''){

        $url = AlfrescoConnect::$webscriptURL.AlfrescoConnect::$userLogin;
        if ($auth) {
            if (!AlfrescoConnect::Authenticate()) return -1;
            $url .= "?alf_ticket=".AlfrescoConnect::$ticket;
        }
        $parameter = array('username' => $username);
//      
        $response = AlfrescoConnect::Query($url, $parameter,HttpRequest::METH_POST,1,0);fb($url);fb($parameter);
        fb($response,'custom authentication result');

        return $response;
    }

    * perform query on alfresco webscripts
    *
    * @param    string  $url  alfresco webscript url
    * @param    string  $params parameter for the query
    * @param    string  $meth metohod for http request HTTP_METH_POST/HTTP_METH_GET default HTTP_METH_POST
    * @param    boolean $auth set whether or not to need authentication
    * @return   mixed   -1 for authentication faliure, -2 for query faliure
    */
    static public function Query($url, $params, $meth = HTTP_METH_POST, $auth = 1, $decode = 1, $ticketUser='') {
        //Aggiunto per evitare il caching 
        //fb($url, "Query url");
        //fb($params, "Query params");
        //print_r(debug_backtrace());
        $nocache=time();
        $params['nocache']=$nocache;
        if ($auth) {
            if(strcmp($ticketUser,'')==0){

                if (!AlfrescoConnect::Authenticate()) return -1;
                $params['alf_ticket'] = AlfrescoConnect::$ticket;
            }
            else{

                $params['alf_ticket'] = $ticketUser;
            }

        }
        fb($params,'param');
        try {
            //fb("invio della richiesta");
            $url=$url."?nocache=$nocache";
            $r= new HttpRequest($url, $meth);
            $r->addQueryData($params);
            $r->send() ;
            fb($r);
            $http_response = $r->getResponseBody();
            fb($http_response);
            if (!$decode) return $http_response;


            $json = new Services_JSON();

            return object2array($json->decode($http_response));

            //return json_decode($http_response, true);
        }
        catch (Exception $e) {
            return -2;
        }
    }   

    /**
     * Azioni su Alfresco
     * 
     */

    static public function createNDG($ndg, $user){
        $url = AlfrescoConnect::$webscriptURL.AlfrescoConnect::$createNDG;
        fb("Crea l'ndg,$url");
        if (strcmp($user,'')!=0) {
            $ticket = AlfrescoConnect::Authenticate($user);
            if (!$ticket) return -1;
        }
        $parameter = array(ModelParameter::$ndg => $ndg);

        $response = AlfrescoConnect::Query($url, $parameter,HTTP_METH_POST,1,1,AlfrescoConnect::$userTicket);

        return $response;

    }

}
Was it helpful?

Solution

It was a memory problem. Never ever try to run Alfresco on a machine with only 1GB of memory!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top