Esempio n. 1
0
 public static function run()
 {
     $o = self::getInstance();
     $SID = $_COOKIE["SID"];
     if (!$SID) {
         return null;
     }
     try {
         if ($SID && !preg_match("/^[a-z0-9]+\$/", $SID)) {
             throw new Exception("SID contains incorrect characters");
         }
         $SID = preg_replace("/[^a-z0-9]/", "", $SID);
         if (!$SID) {
             throw new Exception("SID is empty");
         }
         if ($rw = DB::f1("select * from users_sessions where sid=:SID", array("SID" => $SID))) {
             $rw_session = $rw;
         } else {
             throw new Exception("Auth session not found");
         }
         $Q = new UsersExec();
         $Q->where("id", $rw_session["user_id"]);
         if ($rw = $Q->f1()) {
             $user = $rw;
             $user["rw_session"] = $rw;
             if ($user["settings"]["rememberme"]) {
                 setcookie("SID", $SID, time() + $o->CONFIG["rememberme_time"], "/", $o->CONFIG["cookie_domain"]);
             } else {
                 setcookie("SID", $SID, 0, "/", $o->CONFIG["cookie_domain"]);
             }
             if ($o->CONFIG["enable_online"]) {
                 $online_file = $o->CONFIG["online_cache_dir"] . "/" . (int) (time() / $o->CONFIG["online_interval"]) % 2 . "/" . $user->id;
                 touch($online_file);
             }
             $o->user = $user;
             return true;
         } else {
             throw new Exception("User id=" . $rw_session["user_id"] . " not found");
         }
     } catch (Exception $e) {
         DEBUG::log("Auth Exception: " . $e->getMessage(), __CLASS__);
         if ($SID) {
             DB::q("delete from users_sessions where sid=:SID", array("SID" => $SID));
         }
         $_COOKIE["SID"] = "";
         setcookie("SID", "", time() - 86400, "/", $o->CONFIG["cookie_domain"]);
     }
 }
Esempio n. 2
0
 /**
  * delete this object from the database and cache
  *
  * @return bool
  * @author Craig Ulliott
  */
 public final function delete()
 {
     $this->requiresState();
     $this->checkSet();
     $sql = 'delete from ' . $this->database . '.' . $this->table . ' where ' . $this->primary_key . ' = ' . $this->ID;
     DB::q($sql, __LINE__, __FILE__);
     //do we have a post delete function
     if (method_exists($this, 'postDelete')) {
         $this->postDelete();
     }
     $this->clearCache();
     //remove the ID to disable the object
     unset($this->ID);
     //deleted
     return true;
 }
 public function delete($id, $params)
 {
     App::import('model', 'DB');
     $DB = new DB();
     if (is_string($id)) {
         $id = array($id);
     }
     foreach ($id as &$i) {
         $i = addslashes($i);
     }
     $items = $DB->selectAssocs("SELECT `id`, `alphaid`, `saved` FROM `pisma_documents` WHERE `alphaid`='" . implode("' OR `alphaid`='", $id) . "' AND `from_user_type`='" . addslashes($params['from_user_type']) . "' AND `from_user_id`='" . addslashes($params['from_user_id']) . "'");
     if ($items) {
         foreach ($items as $item) {
             $DB->q("UPDATE `pisma_documents` SET `deleted`='1', `deleted_at`=NOW() WHERE `id`='" . $item['id'] . "' LIMIT 1");
             $ES = ConnectionManager::getDataSource('MPSearch');
             $deleteParams = array();
             $deleteParams['index'] = 'mojepanstwo_v1';
             $deleteParams['type'] = 'letters';
             $deleteParams['id'] = $item['alphaid'];
             $deleteParams['refresh'] = true;
             $deleteParams['ignore'] = array(404);
             $ES->API->delete($deleteParams);
         }
         return 200;
     } else {
         return 404;
     }
 }
            continue;
        }
        $query = "\n\t\t\tinsert into users(`email`,`password`,`sex`,`birth`,`nick`,`about`,`dt_added`,`photo`,`country_id`,`city_id`,`source_id`)\n\t\t\tvalues(:email,:password,:sex,:birth,:nick,:about,:dt_added,:photo,:country_id,:city_id,:source_id)\n\t\t";
        $ar = array("email" => $data["source_id"] . "@noemail.com", "password" => "123123", "sex" => $i + 1, "birth" => (int) (date("Y") - $data["age"]) . "-01-01", "nick" => $data["name"], "about" => $data["about"], "dt_added" => time(), "photo" => "", "country_id" => $data["country_id"], "city_id" => $data["city_id"], "source_id" => $data["source_id"]);
        if ($new_user_id = DB::q($query, $ar)) {
            echo "{$new_user_id} {$data['name']} - added<br>\n";
        } else {
            echo "\n\n<br><br>\n\nFAILED ADDING:\n<br>\n";
            print_r($data);
            echo "<br><br>\n\n";
            break;
        }
        $path = $CONFIG["users_avatars_global_dir"] . "/" . $new_user_id;
        if (!is_dir($path)) {
            mkdir($path, 0777, 1);
        }
        if ($ph = file_get_contents($data["photo"])) {
            $f = fopen($path . "/" . $new_user_id . ".jpg", "w");
            fwrite($f, $ph);
            fclose($f);
        }
        if ($ph = file_get_contents($data["photo_sq"])) {
            $f = fopen($path . "/" . $new_user_id . "_sq.jpg", "w");
            fwrite($f, $ph);
            fclose($f);
        }
        DB::q("update users set photo=:photo where id=:id", array("id" => $new_user_id, "photo" => $CONFIG["users_avatars_local_dir"] . "/" . $new_user_id . "/" . $new_user_id . ".jpg"));
    }
}
DEBUG::finish();
DEBUG::out($CONFIG["debug_format"]);
    public function getBudgetData2($gmina_id = null)
    {
        App::import('model', 'DB');
        $DB = new DB();
        // Configure::write('debug', 2);
        // parametry zewnetrzne
        $data = '2014Q2';
        $gmina = $DB->selectAssoc("SELECT id, nazwa, teryt FROM pl_gminy WHERE id='{$gmina_id}'");
        $teryt = $gmina['teryt'];
        // Przedzia³y wielkoœci gmin
        $ranges = array();
        $ranges[] = array('min' => 0, 'max' => 20000);
        $ranges[] = array('min' => 20000, 'max' => 50000);
        $ranges[] = array('min' => 50000, 'max' => 100000);
        $ranges[] = array('min' => 100000, 'max' => 500000);
        $ranges[] = array('min' => 500000, 'max' => 999999999);
        $data = explode('q', strtolower($data));
        $rok = substr($data[0], 2, 2);
        $miesiac = $data[1];
        $minLiczba = null;
        $maxLiczba = null;
        $liczbaLudnosci = null;
        // Dane podstawowe/globalne
        $sql = sprintf('
			SELECT
				d.id as \'dzial_id\', dzial,
				min, g1.nazwa AS min_nazwa,
				max, g2.nazwa AS max_nazwa,
				sum_section, d.tresc
			FROM finance_date f
			JOIN pl_budzety_wydatki_dzialy d ON d.src = f.dzial
			LEFT JOIN pl_gminy g1 ON g1.teryt = min_teryt
			LEFT JOIN pl_gminy g2 ON g2.teryt = max_teryt
			WHERE rok = %d AND kwartal = %d
			ORDER BY sum_section DESC', $rok, $miesiac);
        $result = $DB->q($sql);
        $results = array();
        $sum = 0;
        while ($row = $result->fetch_assoc()) {
            $results[$row['dzial']] = $row;
            $results[$row['dzial']]['buckets'] = array_fill(0, 10, null);
            $sum += $row['sum_section'];
        }
        $this->_getHistogram($DB, $results, 'buckets', $rok, $miesiac);
        // Jezeli mamy okreslona gmine
        if ($teryt) {
            // dane dla gminy
            $sql = sprintf("\n\t\t\t\tSELECT\n\t\t\t\t\tdzial, sum_section, liczba_ludnosci\n\t\t\t\tFROM finance_teryt\n\t\t\t\tWHERE rok = %d AND kwartal = %d AND teryt = '%s'", $rok, $miesiac, $teryt);
            $result = $DB->q($sql);
            $terytSum = 0;
            $dzial = array();
            while ($row = $result->fetch_assoc()) {
                $dzial[] = $row['dzial'];
                $results[$row['dzial']]['teryt_buckets'] = array_fill(0, 10, null);
                $results[$row['dzial']]['teryt_sum_section'] = $row['sum_section'];
                $terytSum += $row['sum_section'];
                if ($liczbaLudnosci == null) {
                    $liczbaLudnosci = $row['liczba_ludnosci'];
                }
            }
            // Dane sumaryczne dla gminy
            foreach ($dzial as $_dzial) {
                $results[$_dzial]['teryt_sum'] = $terytSum;
                $results[$_dzial]['teryt_sum_section_percent'] = !$terytSum ? 0 : round(100 * $results[$_dzial]['teryt_sum_section'] / $terytSum, 2);
            }
            // Dane dla gmin o podobnej wielkosci
            if ($liczbaLudnosci != null) {
                foreach ($ranges as $range) {
                    if ($liczbaLudnosci >= $range['min'] && $liczbaLudnosci < $range['max']) {
                        $minLiczba = $range['min'];
                        $maxLiczba = $range['max'];
                    }
                }
                $this->_getHistogram($DB, $results, 'teryt_buckets', $rok, $miesiac, $minLiczba, $maxLiczba);
                $sql = sprintf("\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tdzial,\n\t\t\t\t\t\tmin_sum_section, min_teryt, g1.nazwa AS min_teryt_name,\n\t\t\t\t\t\tmax_sum_section, max_teryt, g2.nazwa AS max_teryt_name\n\t\t\t\t\tFROM (\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tdzial,\n\t\t\t\t\t\t\tmin_sum_section, LPAD(IF(min_teryt %% 100 = 0, min_teryt + 1, min_teryt), 6, '0') AS min_teryt,\n\t\t\t\t\t\t\tmax_sum_section, LPAD(IF(max_teryt %% 100 = 0, max_teryt + 1, max_teryt), 6, '0') AS max_teryt\n\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tdzial,\n\t\t\t\t\t\t\t\tMIN(sum_section) AS min_sum_section,\n\t\t\t\t\t\t\t\tIF(LOCATE(',', GROUP_CONCAT(teryt ORDER BY sum_section ASC)) > 0, SUBSTRING(GROUP_CONCAT(teryt ORDER BY sum_section ASC), 1, LOCATE(',',GROUP_CONCAT(teryt ORDER BY sum_section ASC)) - 1), teryt) AS min_teryt,\n\t\t\t\t\t\t\t\tMAX(sum_section) AS max_sum_section,\n\t\t\t\t\t\t\t\tIF(LOCATE(',', GROUP_CONCAT(teryt ORDER BY sum_section DESC)) > 0, SUBSTRING(GROUP_CONCAT(teryt ORDER BY sum_section DESC), 1, LOCATE(',',GROUP_CONCAT(teryt ORDER BY sum_section DESC)) - 1), teryt) AS max_teryt\n\t\t\t\t\t\t\tFROM finance_teryt\n\t\t\t\t\t\t\tWHERE rok = %d AND kwartal = %d  AND liczba_ludnosci >= %d AND liczba_ludnosci < %d\n\t\t\t\t\t\t\tGROUP BY dzial\n\t\t\t\t\t\t) AS ww\n\t\t\t\t\t) AS xx\n\t\t\t\t\tLEFT JOIN pl_gminy g1 ON g1.teryt = min_teryt\n\t\t\t\t\tLEFT JOIN pl_gminy g2 ON g2.teryt = max_teryt", $rok, $miesiac, $minLiczba, $maxLiczba);
                $result = $DB->q($sql);
                while ($row = $result->fetch_assoc()) {
                    $results[$row['dzial']]['teryt_min_sum_section'] = $row['min_sum_section'];
                    $results[$row['dzial']]['teryt_max_sum_section'] = $row['max_sum_section'];
                    $results[$row['dzial']]['teryt_min_nazwa'] = $row['min_teryt_name'];
                    $results[$row['dzial']]['teryt_max_nazwa'] = $row['max_teryt_name'];
                }
                // Gmina na tle podobnych w kazdej kategorii
                foreach ($dzial as $_dzial) {
                    $left = $results[$_dzial]['teryt_min_sum_section'];
                    $right = $results[$_dzial]['teryt_max_sum_section'];
                    $v = $results[$_dzial]['teryt_sum_section'];
                    $results[$_dzial]['teryt_section_percent'] = round(100 * ($v - $left) / ($right - $left));
                }
            }
        }
        // Wynik finalny
        $finalResult = array('sections' => array(), 'stats' => array('sum' => $sum, 'min_liczba_ludnosci' => $minLiczba, 'max_liczba_ludnosci' => $maxLiczba, 'teryt_liczba_ludnosci' => $liczbaLudnosci, 'teryt_nazwa' => @$gmina['nazwa']));
        foreach ($results as $item) {
            $finalResult['sections'][] = array('id' => $item['dzial_id'], 'nazwa' => @$item['tresc'], 'min' => @$item['min'], 'max' => @$item['max'], 'min_nazwa' => @$item['min_nazwa'], 'max_nazwa' => @$item['max_nazwa'], 'sum_section' => @$item['sum_section'], 'buckets' => @$item['buckets'], 'teryt_sum' => @$item['teryt_sum'], 'teryt_sum_section' => @$item['teryt_sum_section'], 'teryt_sum_section_percent' => @$item['teryt_sum_section_percent'], 'teryt_min' => @$item['teryt_min_sum_section'], 'teryt_max' => @$item['teryt_max_sum_section'], 'teryt_section_percent' => @$item['teryt_section_percent'], 'teryt_min_nazwa' => @$item['teryt_min_nazwa'], 'teryt_max_nazwa' => @$item['teryt_max_nazwa'], 'teryt_buckets' => @$item['teryt_buckets']);
        }
        //debug($finalResult); die();
        $finalResult['gmina'] = $gmina;
        return $finalResult;
    }
Esempio n. 6
0
 public function deletePost($id)
 {
     $sql = 'delete from posts where id=?';
     return DB::q($sql, $id);
 }
Esempio n. 7
0
 /**
  * if the dataabse was created then try a simple query that now deletes it, main reason we do this is to have a clean setup for
  * the next time we want to run this test
  * 
  * @depends testCreateDatabase
  */
 public function testDeleteDatabase()
 {
     $sql = 'drop database ' . $this->db_name;
     $result = DB::q($sql);
     // on success we should get back boolean true
     $this->assertTrue($result === TRUE);
 }
 protected function action_write_message_do()
 {
     $formData = $_REQUEST["formData"];
     $to_user_id = (int) $_REQUEST["to"];
     if (!$to_user_id) {
         throw new Exception("<!--[No_user]-->");
     }
     $Q = new UsersExec();
     $Q->where("id", $to_user_id);
     $rw = $Q->f1();
     if (!$rw) {
         throw new Exception("<!--[User]--> " . $to_user_id . " doesn't exist");
     }
     if (!$formData["title"]) {
         throw new Exception("<!--[Enter_message_title]-->");
     }
     if (!$formData["text"]) {
         throw new Exception("<!--[Enter_message_text]-->");
     }
     if (DB::q("insert into messages(`id`,`from`,`to`,`title`,`text`,`dt`) values('',:from,:to,:title,:text,:dt)", array("from" => $this->app->getUser("id"), "to" => $to_user_id, "title" => $formData["title"], "text" => $formData["text"], "dt" => time()))) {
         $this->app->setSuccess("<!--[Message_sent]-->", 5, $this->app->makeLink(array("CID" => "people", "user_id" => $to_user_id)));
         return array("success" => "<!--[Message_sent]-->", "return" => true);
     } else {
         throw new Exception("<!--[Failed_sending_message]-->");
     }
 }
Esempio n. 9
0
 public function delete($keyName = null)
 {
     if (!$this->checkKey($keyName)) {
         $keyName = $this->getPrimKey();
     }
     $sql = sprintf('DELETE FROM `%s` WHERE `%s` = %s', $this->name, $keyName, $this->{$keyName}());
     return DB::q($sql);
 }
Esempio n. 10
0
 public static function getLastInsertId()
 {
     $result = DB::q('select last_insert_id()');
     if ($row = $result->fetch_array()) {
         return $row[0];
     } else {
         return false;
     }
 }
Esempio n. 11
0
 /**
  * The garbage collector, this is executed when the session garbage collector is executed and takes the max session lifetime as its only parameter.
  *
  * @return bool
  * @author Ken Mickles
  */
 public static function gc()
 {
     // delete if (creation_time + lifetime) < current_time
     $sql = 'DELETE FROM ' . SESSION_DB . '.sessions WHERE created between NOW() - INTERVAL 1 YEAR and NOW() - INTERVAL ' . (int) self::$lifetime . ' SECOND';
     return (bool) DB::q($sql, __LINE__, __FILE__);
 }