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;
 }