80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
|
<?php
|
||
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
|
||
|
class RedirectModel extends CI_Model {
|
||
|
|
||
|
public function __construct() {
|
||
|
parent::__construct();
|
||
|
$this->load->model('blogModel', '', TRUE);
|
||
|
}
|
||
|
|
||
|
public function insertRedirect($url, $name, $temp = false, $expireDate = null) {
|
||
|
$result1 = $this->db->query('SELECT * FROM redirects WHERE redirect = ?', [$name])->result_array();
|
||
|
|
||
|
if(empty($result1)) {
|
||
|
$this->db->query('INSERT INTO redirects (url, redirect, temporary, expireDate) VALUES (?, ?, ?, ?)', [$url, $name, $temp, $expireDate]);
|
||
|
return ['feedback' => 'success'];
|
||
|
} else {
|
||
|
return ['feedback' => 'error', 'message' => 'Umleitungs-Code ist schon vergeben!'];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function editRedirect($id, $url, $name) {
|
||
|
$this->db->query('UPDATE redirects SET url = ? WHERE ID = ? AND redirect = ?', [$url, $id, $name]);
|
||
|
}
|
||
|
|
||
|
public function removeRedirect($id) {
|
||
|
$this->db->query('DELETE FROM redirects WHERE ID = ?', [$id]);
|
||
|
}
|
||
|
|
||
|
public function getUrl($name) {
|
||
|
$result1 = $this->db->query('SELECT * FROM redirects WHERE redirect = ?', [$name])->result_array();
|
||
|
|
||
|
if(empty($result1)) {
|
||
|
return base_url();
|
||
|
} else {
|
||
|
$result = $result1[0];
|
||
|
return $result['url'];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function getItems() {
|
||
|
$result = $this->db->query('SELECT * FROM redirects')->result_array();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function getName($url) {
|
||
|
$result = $this->db->query('SELECT * FROM redirects WHERE url = ?', [$url])->result_array();
|
||
|
|
||
|
if(empty($result)) {
|
||
|
return '';
|
||
|
} else {
|
||
|
$result1 = $result[0];
|
||
|
return $result1['redirect'];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function addRandomItem($url, $temporary, $temporaryTime) {
|
||
|
$alreadyExists = $this->getName($url);
|
||
|
if($alreadyExists == '') {
|
||
|
for($i = 0; $i < 1; $i++) {
|
||
|
$randomName = base64_encode($url);
|
||
|
$name = '';
|
||
|
for($j = 0; $j < 5; $j++) {
|
||
|
$random = rand(0, strlen($randomName) - 1);
|
||
|
$name .= $randomName[$random];
|
||
|
}
|
||
|
$expireDate = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . $temporaryTime . ' hours'));
|
||
|
if($this->insertRedirect($url, $name, $temporary, $expireDate)['feedback'] == 'success') {
|
||
|
return $this->getName($url);
|
||
|
} else {
|
||
|
$i--;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
return $alreadyExists;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|