Example #1
0
 public function uploadLink()
 {
     //Define link-variables
     $links = $_POST['links']['link'];
     $names = $_POST['links']['name'];
     $descriptions = $_POST['links']['description'];
     //Define error-array
     $errors = array('error' => false, 'messages' => array());
     //Verify captcha
     $captcha = \Linkadept\Misc::verifyCaptcha();
     if ($captcha['error'] == true) {
         $this->pushError('You must complete the captcha.');
         return $this->errors;
     }
     //LINK VALIDATION
     foreach ($links as $id => $link) {
         //Remove empty links
         if (empty($link)) {
             unset($links[$id]);
             unset($names[$id]);
             unset($descriptions[$id]);
             continue;
         }
         //Trim whitespace
         $links[$id] = trim($links[$id]);
         //Check for valid links
         if (!filter_var($link, FILTER_VALIDATE_URL)) {
             $this->pushError("Link #{$id} must be a valid link (http://www.example.com)");
         }
         //Check for valid link length
         if (strlen($link) > 2048) {
             $this->pushError("Link #{$id} may not exceed 2048 characters");
         }
     }
     //NAME VALIDATION
     foreach ($names as $id => $name) {
         //Trim whitespace
         $names[$id] = trim($names[$id]);
         //Validate name length (255)
         if (strlen($name) > 255) {
             $this->pushError("Name of link #{$id} may not exceed 255 characters");
         }
     }
     //DESCRIPTION VALIDATION
     foreach ($descriptions as $id => $description) {
         //Trim whitespace
         $descriptions[$id] = trim($descriptions[$id]);
         //Validate description length (255)
         if (strlen($description) > 255) {
             $this->pushError("Description of link #{$id} may not exceed 255 characters");
         }
     }
     //Check and display errors
     if ($this->errors['error'] == true) {
         return $this->errors;
     }
     //Fetch last inserted group key
     $q = $this->db->prepare('SELECT group_key FROM group_stats WHERE unique_name = 0 ORDER BY group_stats_id DESC LIMIT 1');
     $q->execute();
     $q = $q->fetch();
     //Set new group key
     $tempGroupKey = \Linkadept\Misc::base64ToBase10($q['group_key']);
     //Loop and check until group key is unique (should be instant)
     do {
         $tempGroupKey = \Linkadept\Misc::base10ToBase64($tempGroupKey + 1);
         $q = $this->db->prepare('SELECT 1 FROM group_stats WHERE group_key=?');
         $q->execute(array($tempGroupKey));
     } while ($q->fetch());
     $groupKey = $tempGroupKey;
     $this->groupKey = $groupKey;
     unset($tempGroupKey);
     //Create group_stats row
     $q = $this->db->prepare('INSERT INTO group_stats (group_key, unique_name) VALUES (:groupKey, :isUnique)');
     $q->execute(array(':groupKey' => $groupKey, ':isUnique' => 0));
     foreach ($links as $id => $link) {
         //Insert links
         $q = $this->db->prepare('INSERT IGNORE INTO links (link) VALUES (?)');
         $q->execute(array($link));
         //Insert groups
         $linkId = $this->db->lastInsertId();
         $q = $this->db->prepare('INSERT INTO groups(group_key, link_id, link_name, link_description) VALUES (:groupKey, :linkId, :name, :description)');
         $q->execute(array('groupKey' => $groupKey, ':linkId' => $linkId, ':name' => $names[$id], ':description' => $descriptions[$id]));
     }
     return $this->errors;
 }
Example #2
0
<?php

$db = \Linkadept\Misc::getDatabaseObj();
$q = $db->prepare('INSERT INTO group_stats_ips_archive (
    group_stats_ips_archive.group_key,
    group_stats_ips_archive.visited_ip,
    group_stats_ips_archive.date)
    SELECT
    group_stats_ips.group_key,
    group_stats_ips.visited_ip,
    group_stats_ips.date
    FROM
    group_stats_ips
    WHERE
    group_stats_ips.date< DATE_SUB( utc_timestamp(), INTERVAL 1 DAY );');
$q->execute();
$q = $db->prepare('DELETE FROM group_stats_ips WHERE group_stats_ips.date < DATE_SUB( utc_timestamp(), INTERVAL 1 DAY );');
$q->execute();
Example #3
0
<?php

require_once 'vendor/autoload.php';
$title = 'Group and shorten links with Linkadept.com';
$announcement = \Linkadept\LocalData::getAnnouncement();
$stylesheet = 'error.css';
$popularLinks = \Linkadept\Misc::getPopularLinks();
$pageVars = array('title' => $title, 'announcement' => $announcement, 'stylesheet' => $stylesheet, 'popularLinks' => $popularLinks);
//Set up Twig Templating Engine
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, array('cache' => 'views/cache', 'debug' => true));
//Define error messages
$httpErrorMessages = array(0 => 'Something went wrong, please re-try and contact us if this persists.', 403 => 'You\'re not allowed access to this page.', 404 => 'The page you requested was not found.', 500 => 'Server error, please re-try or wait.');
//Define default values
$httpErrorCode = 0;
$httpErrorMessage = $httpErrorMessages[0];
if (isset($_GET['httpError'])) {
    $httpErrorCode = $_GET['httpError'];
}
if (isset($httpErrorMessages[$httpErrorCode])) {
    $httpErrorMessage = $httpErrorMessages[$httpErrorCode];
}
$error = array('error' => $httpErrorCode, 'errorMessage' => $httpErrorMessage);
echo $twig->render('View.httpError.html', ['pageVars' => $pageVars, 'error' => $error]);