Example #1
0
 /**
  * Send API Call
  *
  * @param	string		Method
  * @param	array		Arguments
  * @return	stdClass	Object from returned JSON
  */
 public function __call($method, $args)
 {
     $send = array_merge(array('key' => $this->api_key), (isset($args[0]) and is_array($args[0])) ? $args[0] : array());
     if (IPS_DOC_CHAR_SET != 'UTF-8') {
         $send['message']['html'] = IPSText::convertCharsets($send['message']['html'], IPS_DOC_CHAR_SET, 'utf-8');
     }
     $response = $this->cfm->postFileContents(self::URL . str_replace('_', '/', $method) . '.json', json_encode($send));
     if ($json = json_decode($response)) {
         return $json;
     }
     return NULL;
 }
Example #2
0
 /**
  * Send batch to the backup server
  */
 public function sendBatch()
 {
     /* Fetch kernel class */
     require_once IPS_KERNEL_PATH . 'classFileManagement.php';
     $cfm = new classFileManagement();
     /* Fetch DB data */
     $tables = $this->_getDatabaseSchematics();
     $sqlRows = array();
     $touchedTables = array();
     $bytesUsed = '';
     $topId = 0;
     $delIds = array();
     /* Do we need to restart? */
     if ($this->checkForRestart()) {
         $this->populateLogWithAllTables();
         return true;
     }
     /* Little short cut */
     $p = $this->registry->dbFunctions()->getPrefix();
     /* Start looping on the table */
     $this->DB->build(array('select' => '*', 'from' => 'backup_queue', 'order' => 'queue_id', 'limit' => array(0, $this->Limits['rows'])));
     $o = $this->DB->execute();
     while ($row = $this->DB->fetch($o)) {
         $tbl = $this->_stripPrefix($row['queue_entry_table']);
         $sql = '';
         $fields = array();
         $touchedTables[$tbl] = $tbl;
         if ($row['queue_entry_sql']) {
             $sql = $row['queue_entry_sql'];
         } else {
             /* Get fields */
             foreach ($tables[$row['queue_entry_table']]['cols'] as $col) {
                 $fields[] = $col['Field'];
             }
             if (!count($fields)) {
                 /* Empty - so remove this queue row or we'll never progress */
                 $delIds[] = $row['queue_id'];
                 continue;
             }
             /* INSERT */
             if ($row['queue_entry_type'] == 1) {
                 /* Get data */
                 $pKey = $row['queue_entry_key'];
                 $pKeyVal = $this->_isConcat($pKey) ? addslashes($row['queue_entry_value']) : $row['queue_entry_value'];
                 $data = array();
                 $vals = array();
                 $data = $this->DB->buildAndFetch(array('select' => '`' . implode("`, `", $fields) . '`', 'from' => $tbl, 'where' => $pKey . "='" . $pKeyVal . "'"));
                 if (!is_array($data) or !count($data)) {
                     /* Empty - so remove this queue row or we'll never progress */
                     $delIds[] = $row['queue_id'];
                     continue;
                 }
                 foreach ($data as $k => $v) {
                     $vals[] = $this->_makeValueSafeForQuery($v);
                 }
                 $sql = "INSERT INTO " . $p . $tbl . "(`" . implode("`, `", $fields) . "`) " . "VALUES( '" . implode("', '", $vals) . "');";
             }
         }
         /* Got anything? */
         if (!$sql) {
             /* Empty - so remove this queue row or we'll never progress */
             $delIds[] = $row['queue_id'];
             continue;
         }
         /* check size */
         $bytesUsed += IPSLib::strlenToBytes(strlen($sql));
         /* Truth time! */
         if ($bytesUsed >= $this->Limits['bytes']) {
             break;
         } else {
             $sqlRows[] = $sql;
             /* top id to remove */
             $topId = $row['queue_id'];
         }
     }
     /* Anything to delete? */
     if (count($delIds)) {
         $this->DB->delete('backup_queue', 'queue_id IN (' . implode(',', array_values($delIds)) . ')');
     }
     /* What do we have? */
     if ($topId && count($sqlRows)) {
         $dataToSend = $this->_createTextToSend($sqlRows, array_keys($touchedTables));
         $returnedData = $cfm->postFileContents($this->_getBackupServerUrl(), array('backup_data' => @gzcompress($dataToSend), 'lkey' => ipsRegistry::$settings['ipb_reg_number']));
         $test = json_decode(trim($returnedData), true);
         if (is_array($test) && $test['status'] == 'ok') {
             $this->DB->delete('backup_queue', 'queue_id <= ' . intval($topId));
             $this->_addLog(intval($test['rows']), $test['status']);
             $this->Vars['rows_sent'] = intval($this->Vars['rows_sent']) + count($sqlRows);
             $this->Vars['rows_total'] = $this->_getStoredRowCount();
             $this->_setVars();
         } else {
             # Fail lol
             $this->_addLog(0, $test['status']);
         }
     }
 }