/**
  * Add pages
  *
  * @param Array $data
  * @return int
  */
 public function addPage($userID, $data)
 {
     global $db;
     if (!is_numeric($userID) || $data['pageName'] == '') {
         return;
     }
     // failed
     //Create Links
     $links = [];
     if (isset($data['title'])) {
         foreach ($data['title'] as $i => $title) {
             $links[] = ['title' => trim(strip_tags($title)), 'link' => trim(strip_tags($data['url'][$i]))];
         }
     }
     //Move Image File
     list($width, $height, $type, $attr) = getimagesize(DIR_FS_PHOTO_TMP . $data['file']);
     if ($width > MAX_IMAGE_WIDTH || $height > MAX_IMAGE_HEIGHT) {
         buckys_add_message(MSG_PHOTO_MAX_SIZE_ERROR, MSG_TYPE_ERROR);
         return false;
     }
     $ratio = floatval($width / $data['width']);
     $sourceWidth = ($data['x2'] - $data['x1']) * $ratio;
     BuckysPost::moveFileFromTmpToUserFolder($userID, $data['file'], PROFILE_IMAGE_WIDTH, PROFILE_IMAGE_HEIGHT, $data['x1'] * $ratio, $data['y1'] * $ratio, $sourceWidth, $sourceWidth);
     $query = $db->prepare("INSERT INTO " . TABLE_PAGES . "(`userID`, `title`, `logo`, `about`, `links`, `createdDate`, `status`)\n                            VALUES(%d, %s, %s, %s, %s, %s, 1)", $userID, $data['pageName'], $data['file'], $data['pageDescription'], serialize($links), date('Y-m-d H:i:s'));
     if (!($newID = $db->insert($query))) {
         buckys_add_message($db->getLastError(), MSG_TYPE_ERROR);
     }
     return $newID;
 }
 /**
  * Save Comment
  *
  * @param Int    $userID
  * @param Int    $postID
  * @param String $comment
  * @return int|null|string
  */
 public static function saveComments($userID, $postID, $comment, $image = null)
 {
     global $db;
     $now = date("Y-m-d H:i:s");
     if ($image != null) {
         if (file_exists(DIR_FS_PHOTO_TMP . $image)) {
             list($width, $height, $type, $attr) = getimagesize(DIR_FS_PHOTO_TMP . $image);
             if ($width > MAX_COMMENT_IMAGE_WIDTH) {
                 $height = $height * (MAX_COMMENT_IMAGE_WIDTH / $width);
                 $width = MAX_COMMENT_IMAGE_WIDTH;
             }
             if ($height > MAX_COMMENT_IMAGE_HEIGHT) {
                 $width = $width * (MAX_COMMENT_IMAGE_HEIGHT / $height);
                 $height = MAX_COMMENT_IMAGE_HEIGHT;
             }
             BuckysPost::moveFileFromTmpToUserFolder($userID, $image, $width, $height, 0, 0);
         } else {
             $image = null;
         }
     }
     $newId = $db->insertFromArray(TABLE_COMMENTS, ['postID' => $postID, 'commenter' => $userID, 'content' => $comment, 'image' => $image, 'posted_date' => $now]);
     if (buckys_not_null($newId)) {
         $postData = BuckysPost::getPostById($postID);
         BuckysUsersDailyActivity::addComment($userID);
         //Update comments on the posts table
         $query = $db->prepare('UPDATE ' . TABLE_POSTS . ' SET `comments`=`comments` + 1 WHERE postID=%d', $postID);
         $db->query($query);
         //Add Activity
         $activityID = BuckysActivity::addActivity($userID, $postID, 'post', 'comment', $newId);
         //Add Notification
         if ($postData['poster'] != $userID) {
             BuckysActivity::addNotification($postData['poster'], $activityID, BuckysActivity::NOTIFICATION_TYPE_COMMENT_TO_POST);
         }
         //Get Already Commented users which commentToComment is 1
         $query = $db->prepare("SELECT DISTINCT(pc.commenter), IFNULL(un.notifyCommentToMyComment, 1) AS notifyCommentToMyComment FROM " . TABLE_POSTS_COMMENTS . " AS pc LEFT JOIN " . TABLE_USERS_NOTIFY_SETTINGS . " AS un ON pc.commenter = un.userID WHERE pc.postID=%d AND pc.commenter != %d AND IFNULL(un.notifyCommentToMyComment, 1) > 0", $postID, $userID);
         $rows = $db->getResultsArray($query);
         foreach ($rows as $row) {
             BuckysActivity::addNotification($row['commenter'], $activityID, BuckysActivity::NOTIFICATION_TYPE_COMMENT_TO_COMMENT);
         }
         //Increase Hits
         BuckysHit::addHit($postID, $userID);
         //Update User Stats
         BuckysUser::updateStats($postData['poster'], 'comments', 1);
     }
     return $newId;
 }
 /**
  * Save Post
  *
  * @param $userID
  * @param mixed $data
  * @return bool|int|null|string
  */
 public static function savePhoto($userID, $data)
 {
     global $db, $TNB_GLOBALS;
     //Check the Photo File Name
     if (!isset($data['file']) || strpos($data['file'], "../") !== false || !file_exists(DIR_FS_PHOTO_TMP . $data['file'])) {
         buckys_add_message(MSG_FILE_UPLOAD_ERROR, MSG_TYPE_ERROR);
         return false;
     }
     $data['pageID'] = isset($data['pageID']) && is_numeric($data['pageID']) ? $data['pageID'] : BuckysPost::INDEPENDENT_POST_PAGE_ID;
     // Validate the file type
     $fileParts = pathinfo($data['file']);
     if (!in_array(strtolower($fileParts['extension']), $TNB_GLOBALS['imageTypes'])) {
         buckys_add_message(MSG_INVALID_PHOTO_TYPE, MSG_TYPE_ERROR);
         return false;
     }
     //Validate File Size
     list($width, $height, $type, $attr) = getimagesize(DIR_FS_PHOTO_TMP . $data['file']);
     if ($width * $height > MAX_IMAGE_WIDTH * MAX_IMAGE_HEIGHT) {
         buckys_add_message(MSG_PHOTO_MAX_SIZE_ERROR, MSG_TYPE_ERROR);
         return false;
     }
     //Checking File Size and move it from the tmp folder to the user photo folder and resize it.
     if ($data['post_visibility'] == 2) {
         //Calc Ratio using real image width
         $ratio = floatval($width / $data['width']);
         $sourceWidth = ($data['x2'] - $data['x1']) * $ratio;
         BuckysPost::moveFileFromTmpToUserFolder($userID, $data['file'], PROFILE_IMAGE_WIDTH, PROFILE_IMAGE_HEIGHT, $data['x1'] * $ratio, $data['y1'] * $ratio, $sourceWidth, $sourceWidth);
         if ($data['pageID'] == BuckysPost::INDEPENDENT_POST_PAGE_ID) {
             //Update User Profile Field
             BuckysUser::updateUserFields($userID, ['thumbnail' => $data['file']]);
             $is_profile = 1;
         } else {
             //Update Page Profile field
             $pageIns = new BuckysPage();
             $pageIns->updateData($data['pageID'], ['logo' => $data['file']]);
             $is_profile = 1;
         }
     } else {
         if ($width > MAX_POST_IMAGE_WIDTH) {
             $height = $height * (MAX_POST_IMAGE_WIDTH / $width);
             $width = MAX_POST_IMAGE_WIDTH;
         }
         if ($height > MAX_POST_IMAGE_HEIGHT) {
             $width = $width * (MAX_POST_IMAGE_HEIGHT / $height);
             $height = MAX_POST_IMAGE_HEIGHT;
         }
         //Create normal image
         BuckysPost::moveFileFromTmpToUserFolder($userID, $data['file'], $width, $height, 0, 0);
         $is_profile = 0;
     }
     $now = date('Y-m-d H:i:s');
     $newId = $db->insertFromArray(TABLE_POSTS, ['poster' => $userID, 'pageID' => $data['pageID'], 'profileID' => $data['profileID'], 'content' => $data['content'], 'type' => 'image', 'post_date' => $now, 'image' => $data['file'], 'visibility' => $data['post_visibility'] > 0 ? 1 : 0, 'is_profile' => $is_profile]);
     if (!$newId) {
         buckys_add_message($db->getLastError(), MSG_TYPE_ERROR);
         return false;
     }
     //Assign Photo to Album
     if (isset($data['album']) && $data['album'] != '') {
         if (!BuckysAlbum::checkAlbumOwner($data['album'], $userID)) {
             buckys_add_message(MSG_INVALID_ALBUM_ID, MSG_TYPE_ERROR);
         } else {
             BuckysAlbum::addPhotoToAlbum($data['album'], $newId);
         }
     }
     buckys_add_message(MSG_PHOTO_UPLOADED_SUCCESSFULLY);
     return $newId;
 }