He's off the map, he's off the map! - [entries|archive|friends|userinfo]
kruzulis

[ userinfo | sc userinfo ]
[ archive | journal archive ]

Links
[Links:| bīdīties laboties aptaujāties komentāri skaitļi un fakti random xkcd kruzulis's movies ]

[May. 9th, 2022|01:12 pm]
Previous Entry Add to Memories Tell A Friend Next Entry
Nokārtots

class LiveJournalArchive
{
	private $user;
	private $pass;

	/**
	 * @param mixed $user
	 * @param mixed $pass
	 */
	public function __construct($user, $pass)
	{
		$this->user = $user;
		$this->pass = md5($pass);
		$this->api = 'http://www.klab.lv/interface/flat';
	}

	public function archive($yearFrom, $yearTo)
	{
		foreach (range($yearFrom, $yearTo) as $year) {
			foreach (range(1, 12) as $month) {
				foreach (range(1, 31) as $day) {
					$this->modifyEvents($year, $month, $day);
				}
			}
		}
	}

	private function modifyEvents($year, $month, $day)
	{
		$vars = [
			'mode' => 'getevents',
			'user' => "{$this->user}",
			'ver' => 1,
			'hpassword' => $this->pass,
			'prefersubject' => 0,
			'noprops' => 0,
			'selecttype' => 'day',
			'year' => $year,
			'month' => $month,
			'day' => $day,
			'lineendings' => 'pc',
		];

		$response = $this->httpPost($this->api, $vars);

		$events = $this->parseResponse($response);

		$count = $events['events_count'];
		if ($count > 0) {
			foreach (range(1, $count) as $i) {
				$event = $this->decodeString($events['events_'.$i.'_event']);
				$subject = $events['events_'.$i.'_subject'] ?? null;
				$itemId = $events['events_'.$i.'_itemid'];
				$this->editEvent($itemId, $subject, $event);
			}
		}
	}

	private function decodeString($who): array|string
	{
		$who = str_replace('+', ' ', $who);
		$who = preg_replace_callback(
			'/%([a-fA-F0-9]{2,2})/',
			static function ($matches) {
				$hexStr = preg_replace('/[^0-9A-Fa-f]/', '', $matches[0]); // Gets a proper hex string

				return chr(hexdec($hexStr));
			},
			$who
		);

		return preg_replace('//', '', $who);
	}

	private function editEvent($itemId, $subject, $event)
	{
		$security = 'private'; //usemask|private|public
		$vars = [
			'mode' => 'editevent',
			'user' => $this->user,
			'ver' => 1,
			'hpassword' => $this->pass,
			'itemid' => $itemId,
			'subject' => $subject,
			'event' => $event,
			'lineendings' => 'unix',
			'security' => $security,
			'allowmask' => 0,
		];

		$response = $this->httpPost($this->api, $vars);
		$result = $this->parseResponse($response);

		if ($result['success'] === 'OK') {
			print 'itemId '.$itemId." modified.\n";
		} else {
			print $result['errmsg'];
		}
	}

	private function parseResponse(string $responseText): array
	{
		$response = explode("\n", rtrim($responseText));
		$responseParsed = [];
		foreach ($response as $i => $text) {
			if ($i % 2 === 0) {
				$responseParsed[$text] = $response[$i + 1];
			}
		}

		return $responseParsed;
	}

	private function httpPost($url, $data)
	{
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($curl);
		curl_close($curl);

		return $response;
	}
}

$kl = new LiveJournalArchive('username', 'pass');
$kl->archive(2004, 2022);

link