===== PART 12/12 ===== mit) { $time_zone = get_option('timezone', 'UTC'); // Convert to UTC for database query $start = Time::now($time_zone)->startOfDay()->i18nFormat('yyyy-MM-dd HH:mm:ss', 'UTC', 'en'); $end = Time::now($time_zone)->endOfDay()->i18nFormat('yyyy-MM-dd HH:mm:ss', 'UTC', 'en'); $links_daily_count = $this->Links->find() ->where([ 'user_id' => $user_id, "created BETWEEN :date1 AND :date2", ]) ->bind(':date1', $start, 'datetime') ->bind(':date2', $end, 'datetime') ->count(); if ($links_daily_count >= $user_plan->url_daily_limit) { $content = [ 'status' => 'error', 'message' => __('Your account has exceeded its daily created short links limit.'), 'url' => '', ]; $this->setResponse($this->getResponse()->withStringBody(json_encode($content))); return $this->response; } } if ($user_plan->url_monthly_limit) { $time_zone = get_option('timezone', 'UTC'); // Convert to UTC for database query $start = Time::now($time_zone)->startOfMonth()->i18nFormat('yyyy-MM-dd HH:mm:ss', 'UTC', 'en'); $end = Time::now($time_zone)->endOfMonth()->i18nFormat('yyyy-MM-dd HH:mm:ss', 'UTC', 'en'); $links_monthly_count = $this->Links->find() ->where([ 'user_id' => $user_id, "created BETWEEN :date1 AND :date2", ]) ->bind(':date1', $start, 'datetime') ->bind(':date2', $end, 'datetime') ->count(); if ($links_monthly_count >= $user_plan->url_monthly_limit) { $content = [ 'status' => 'error', 'message' => __('Your account has exceeded its monthly created short links limit.'), 'url' => '', ]; $this->setResponse($this->getResponse()->withStringBody(json_encode($content))); return $this->response; } } $link = $this->Links->newEntity(); $data = []; $data['user_id'] = $user->id; $data['url'] = $this->getRequest()->getData('url'); $data['url_hash'] = sha1($this->getRequest()->getData('url')); $data['domain'] = $domain; if ($user_plan->alias && !empty($this->getRequest()->getData('alias'))) { $data['alias'] = $this->getRequest()->getData('alias'); } else { $data['alias'] = $this->Links->geturl(); } $data['ad_type'] = $this->getRequest()->getData('ad_type'); $link->status = 1; $link->hits = 0; $link->method = 1; $link->last_activity = Time::now(); $linkMeta = [ 'title' => '', 'description' => '', 'image' => '', ]; if ($user_id === 1 && get_option('disable_meta_home') === 'no') { $linkMeta = $this->Links->getLinkMeta($this->getRequest()->getData('url')); } if ($user_id !== 1 && get_option('disable_meta_member') === 'no') { $linkMeta = $this->Links->getLinkMeta($this->getRequest()->getData('url')); } $data['title'] = $linkMeta['title']; $data['description'] = $linkMeta['description']; $link->image = $linkMeta['image']; $link = $this->Links->patchEntity($link, $data); if ($this->Links->save($link)) { $content = [ 'status' => 'success', 'message' => '', 'url' => get_short_url($link->alias, $domain), ]; $this->setResponse($this->getResponse()->withStringBody(json_encode($content))); return $this->response; } $message = __('Invalid URL.'); if ($link->getErrors()) { $error_msg = []; foreach ($link->getErrors() as $errors) { if (is_array($errors)) { foreach ($errors as $error) { $error_msg[] = $error; } } else { $error_msg[] = $errors; } } if (!empty($error_msg)) { $message = implode("
", $error_msg); } } $content = [ 'status' => 'error', 'message' => $message, 'url' => '', ]; $this->setResponse($this->getResponse()->withStringBody(json_encode($content))); return $this->response; } }